ruby on rails 3 - How to set up multiple aliased joins between two models? -
in rails 3.2 app need set 2 associations between same 2 models.
for example
class user has_many :events has_many :attendances has_many :attended_events, through: :attendances end class event belongs_to :event_organizer, class_name: "user" has_many :attendances has_many :attendees, through: :attendances, class_name: "user" end class attendance belongs_to :attended_event, class_name: "event" belongs_to :attendee, class_name: "user" end this first time i've experimented aliasing class names, , i'm having trouble getting work. i'm not sure if issue lies how i've defined associations, or elsewhere.
do associations ok? have overlooked needed work?
the problem i'm having no user ids being set in attendance model. may stupid question, given associations above, should field name :user_id or :event_organizer_id?
really appreciate suggestions on this. thanks!
foreign_key specified in user , event
class user has_many :events has_many :attendances, foreign_key: :attendee_id has_many :attended_events, through: :attendances end class event belongs_to :event_organizer, class_name: "user" # here should event_organizer_id has_many :attendances, foreign_key: :attended_event_id has_many :attendees, through: :attendances end class attendance belongs_to :attended_event, class_name: "event" # should have attended_event_id belongs_to :attendee, class_name: "user" # should have attendee_id because of 1st param belongs_to here "attendee" # , same should added foreign_key in user model # same follows event end
Comments
Post a Comment