ruby-on-railsumlclass-diagramobject-diagram

Modeling an Association Class In Rails - Student Registration Example


I'm trying to model the classic example of a association model and link objects. The image below sets forth a class diagram showing association classes and Object diagram showing link objects.

Association class and link objects

The image shows a part an object diagram representing a student, Mary Jones, and the courses she has registered for in the Fall 2010 term: MKT350 and MIS385. Corresponding to an association class in a class diagram, link objects are present in an object diagram. In this example, there are two link objects (shown as :Registration) for the Registration association class, capturing the two course registrations.

Registration is the Association Class :Registration is the link objects

My question is how would I model something like this in Rails /ActiveRecord


Solution

  • Please read A Guide to Active Record Associations for the association basics, especially has_many :through and has_and_belongs_to_many sections.

    has_and_belongs_to_many is only suitable for you don't need other information except the many-to-many association. You don't need to create a model class for the join table. If you are not sure, use has_many :through instead.

    In your case, you can use has_many :through association. In Registration class, you can use ActiveRecord validation to check eligibility.