Hi again! In my application, I have a Users model and an Events model. I also have a link model to link an event to a user. However, a user can be linked to an event in a number of ways - e.g. they can have created an event, or subscibed to an event. Now, the link table for both these relationships will effectively contain the same information - an event ID and a user ID. How is this link represented in the rails model? For example, can I say: class User < ActiveRecord::Base has_many :created_events has_many :subscribed_events has_many :events, :through => :created_events, :subscribed_events or something to that effect? Also, how would User.events work in this case? The logic doesn't seem quite correct! Have I went about my database design in the wrong way? As always, any help is greatly appreciated - I hope I have made myself clear! Cheers, Mick
on 2007-03-13 12:49
on 2007-03-13 12:58
> case? The logic doesn't seem quite correct! > > Have I went about my database design in the wrong way? > > As always, any help is greatly appreciated - I hope I have made myself > clear! Untested, but fairly sure it will work: Fetch the events for both relationships, then just add them: def events cr_ev=created_events.events sb_ev=subscribed_events.events total = cr_ev + sb_ev total.uniq end Best regards Peter De Berdt
on 2007-03-13 13:16
Could a potential database design be as follows: events - has unique event ID users - has unique user ID user_events - has event ID and user ID as a foreigh key user_event_creation - has unique ID and user_event_id as foreign key user_event_subscription - has unique ID and user_event_id as foreign key any ideas?