Hi, very much a newbie, so please excuse the basic question. Here is what I have: class Author < ActiveRecord::Base has_many :books end class Books < ActiveRecord::Base belongs_to :author end What I am trying to do is find all Authors and list them in the order of how many books they have. It seems that this should be pretty straight-forward, but it''s hard to google for... Any help greatly apprciated! thank you, Olia --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You can try sort method with a block: Author.find(:all).sort {|x,y| x.books.size <=> y.books.size} --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the reply! Unfortunately, I need a way to do it in a query, because I need it to be part of pagination. On Aug 28, 1:00 pm, Vic <victor.mo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You can try sort method with a block: > > Author.find(:all).sort {|x,y| x.books.size <=> y.books.size}--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Try this: Book.find(:all, :select => "author_id, count(id) as book_count", :group => "author_id", :order => "book_count") Regards, Jorge. On Aug 28, 3:50 pm, olia <olia.kerzh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for the reply! > > Unfortunately, I need a way to do it in a query, because I need it to > be part of pagination. > > On Aug 28, 1:00 pm, Vic <victor.mo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > You can try sort method with a block: > > > Author.find(:all).sort {|x,y| x.books.size <=> y.books.size}--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---