0

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

Sled
  • 18,541
  • 27
  • 119
  • 168
Mutuelinvestor
  • 3,384
  • 10
  • 44
  • 75
  • So, what's your problem? Did you tried to write the code and has some problem? Did you read the ActiveRecord association guide? http://guides.rubyonrails.org/association_basics.html – miaout17 Dec 01 '11 at 02:03
  • Yes, I have read the association section of the rails guide and I don't see where it addresses an association class. Most of the time in the guide is devoted to associations between two models. I believe, this is something slightly different. – Mutuelinvestor Dec 01 '11 at 02:08
  • 2
    Please read section 2.4 and 2.6. They are for many-to-many association. – miaout17 Dec 01 '11 at 02:10
  • OK, so you're saying that the "Association Class" is simply the join table between student and course and that the checkElibility() is simply a method in the join table. Is it really that straightforward. – Mutuelinvestor Dec 01 '11 at 02:30
  • 1
    Yes. Posted my answer. I can write some sample code, but I think it will be a good exercise for you. – miaout17 Dec 01 '11 at 03:01
  • One last comment: is it strange to have 1-1 association between account and registration? If a student has 5 courses, he can have 5 different account and password. In general, account should be associated to user (student). – miaout17 Dec 01 '11 at 03:03

1 Answers1

1

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.

miaout17
  • 4,715
  • 2
  • 26
  • 32