Sandy Thomson
2009-Mar-09 16:42 UTC
order by number of occurances in a has_many relationship
I can''t seem to find anything to explain how I might be able to order a list according to the number of occurrences of an item in a has_many relationship, so maybe someone could help me learn how... To illustrate, imagine I have the following situation: I have one table with details for a number of people, and one table of items. Each item belongs to one person and each person can have may items. In my view I have something like this: <% for person in @people %> <tr> <td><%=h person.name %></td> <td><%=h person.items.count %></td> </tr> <% end %> I can get the list to order by the id/name etc. of the people, but I really want to order it by the number of items belonging to each person, something like so: @people = Person.find(:all, :order => ''person.items.count desc'') except that this brings up an ''unknown column'' error. Any ideas for getting around this? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Donald
2009-Mar-09 17:02 UTC
Re: order by number of occurances in a has_many relationship
On Mon, Mar 9, 2009 at 11:42 AM, Sandy Thomson <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> @people = Person.find(:all, :order => ''person.items.count desc'') > > except that this brings up an ''unknown column'' error. > > Any ideas for getting around this?@people.sort!{ |a,b| a.items.count <=> b.items.count } -- Greg Donald http://destiney.com/ --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sandy Thomson
2009-Mar-09 18:00 UTC
Re: order by number of occurances in a has_many relationship
Greg Donald wrote:> On Mon, Mar 9, 2009 at 11:42 AM, Sandy Thomson > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> @people = Person.find(:all, :order => ''person.items.count desc'') >> >> except that this brings up an ''unknown column'' error. >> >> Any ideas for getting around this? > > @people.sort!{ |a,b| a.items.count <=> b.items.count } > > > > -- > Greg Donald > http://destiney.com/Cheers for that Greg, it works fine but is a little heavy on the database (predictable I guess). However, this has steered me towards creating a counter cache in the people table, which I hadn''t considered before and which cuts down hugely on the number of SQL queries. Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---