Re: [Rails] Re: Association count sorting

2010-07-20 Thread Michael Pavling
On 20 July 2010 04:14, badnaam wrote: > Here is *AN* answer if someone else runs into this.. Your solution may certainly work... but is a bit "imperative" in coding terms, and does introduce an n+1 problem. Rails offers a feature called "counter_cache" on associations - which gives you a column

[Rails] Re: Association count sorting

2010-07-19 Thread badnaam
Here is the answer if someone else runs into this.. GrandPa.all do |gpa| p gpa.name gpa.pas.all(:joins => :kids, :select => "pas.*, count(pas.id) AS kid_count" :group => :id, :order => "kid_count DESC").each do |pa| p "#{pa.name} : #{pa.kid_count}" end end On Jul 19, 7:58 pm, Angel

Re: [Rails] Re: Association count sorting

2010-07-19 Thread Angel Robert Marquez
> > Thanks. > > Grandpa has one to many kids and has one pa > No, Grandpa has_many pas > How is this possible? That's like saying I have two left arms. > > I didnt quite get how you proposed listing the pas (and kid count for > each pa) that belong to a certain grandpa > Because the sort is to eas

[Rails] Re: Association count sorting

2010-07-19 Thread badnaam
Thanks. > Grandpa has one to many kids and has one pa No, Grandpa has_many pas I didnt quite get how you proposed listing the pas (and kid count for each pa) that belong to a certain grandpa On Jul 19, 7:42 pm, Angel Robert Marquez wrote: > Kid has one pa and has one grandpa through pa > Pa has