With ActiveRecord, how do I sort results using associations? For example, I have class Roster < ActiveRecord # name => text has_many :members end class Member < ActiveRecord # zip_code => integer belongs_to :roster end I would like to generate a list of members, grouped by roster name, sub-sorted by zip code. Something like this: Member.order("roster.name, zip_code").all But I can''t get the syntax quite right - I''ve played around with join and include - can anyone tell me the right syntax to use?? thanks, Andy -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 16, 2:27 pm, andyl <akl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Something like this: Member.order("roster.name, zip_code").all > > But I can''t get the syntax quite right - I''ve played around with join > and include - can anyone tell me the right syntax to use??Remember that table names are plural. Member.joins(:roster).order("rosters.name, zip_code") -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
That did it ! Thank you very much !!!!! On Nov 16, 11:48 am, Tim Shaffer <timshaf...-BUHhN+a2lJ4@public.gmane.org> wrote:> On Nov 16, 2:27 pm, andyl <akl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Something like this: Member.order("roster.name, zip_code").all > > > But I can''t get the syntax quite right - I''ve played around with join > > and include - can anyone tell me the right syntax to use?? > > Remember that table names are plural. > > Member.joins(:roster).order("rosters.name, zip_code")-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.