views:

217

answers:

1

Hi.

I have a model of a club where I want to model the two entities Meeting and Member.

There are actually two many-to-many relationships between these entities though, as for any meeting a Member can either be a Speaker or a Guest. Now I am an OO thinker, so would normally just create the two classes and each one would just have two arrays of the other inside, but rails is making me think a bit more data centric here, so I realise I need to break these M2M relationships up with join tables Speakers and Guests which I have done, but now I am having trouble describing the relationships in the models.

The two join table models both have "belongs_to :meeting" and "belongs_to :member" and I think that should be sufficient.

I am not however sure about the Meeting and Member models though.

Each one has "has_many :guests" and "has_many: speakers" but I am not sure if I also want to go: has_many :members, :through => :guests has_many :members, :through => :speakers

But I suspect that this is like declaring two "members" that will clash.

I also thought about: has_many :guests, :through => :guests has_many :speakers, :through => :speakers

Does that make sense? How would ActiveRecord know that they are in fact Members?

I have found heaps of examples of polymorphic m2m relationships and m2m relationships where 1 table references itself, but no good examples to help me mode this situation where two separate tables have two different m2m relationships.

Anyone got any tips?

+1  A: 

You need to pick different association names and then specify the model:

class Meeting
  has_many :guests
  has_many :speakers
  has_many :guest_members, :through => :guests, :source => 'Member'
  has_many :speaker_members, :through => :speakers, :source => 'Member'
end
mckeed
Thanks for your reply. I tried that, although :class_name is not supported with :through, using :source works.Now I just need to struggle with the forms, controllers, routers etc. I am not sure if I want to use a Member controller, or Guest and Speaker controllers. The form I have now looks for a Member controller so I will try that first...
Kurt
Oh, right, sorry. I'll edit the answer for future readers. For what it's worth, I would probably design this like `has_many :members, :through => :registrations` and `has_many :speakers, :through => :speaker_registrations, :source => 'Member'`
mckeed