Reputation: 10208
I have a model called Event that has many EventRegistrations, which belongs to a person. Also the EventRegistration has many EventAtendees, which belongs to a person too.
I want to order in Rails all the people related to a event, which means: - Person associated to an event registration - Person associated to an event atendee which is associated to a registration..
Any help?
Upvotes: 0
Views: 65
Reputation: 2746
Here is how you would order by the person's last name, assuming you have all your ActiveRecord associations set up correctly.
event_registrations = EventRegistrations.find(:all)
ordered_event_registrations = event_registrations.sort{|registration_a,registration_b|registration_a.people.lastname<=>registration_b.people.lastname}
Upvotes: 0
Reputation: 9700
As I understand it, you have the following models and associations:
Event
has_many :event_registrations
EventAttendee
belongs_to :event_registration
belongs_to :person
Person
has_many :event_registrations
has_many :event_attendees
EventRegistration
belongs_to :person
belongs_to :event
Now, as to your actual question. You say you want to 'order all the people related to an event'
I don't actually see how 'ordering' (a.k.a sorting) enters into this.
To get all users associated with an event, I recommend adding some :through associations:
Event
has_many :event_attendees, :through => :event_registrations
has_many :people, :through => :event_attendees
Then, when you have an event object, you can simply call .people
on it, and it'll just work.
Upvotes: 2