[Rails] Re: how do I get all records whose count of associations is above a certain number

2010-02-15 Thread Jon Cox
Frank Kim wrote: > I have a model, let's call it Player. > > It has many Trophies. > > How do I do a simple query in Rails in my controller that will return > let's say all Players that have more than 5 tropies? > > Sorry for the dumb question but I can't figure it out. :-) > > -- > Frank Kim >

[Rails] Re: how do I get all records whose count of associations is above a certain number

2010-02-15 Thread Frederick Cheung
On Feb 15, 6:59 am, Peter wrote: > And you can actually represent this in AR friendly syntax; no console > in front of me but its something like > > Player.find(:select => "players.id", :joins => :trophies, :group => > 'players.id', :having => 'count(players.id) > 5') > > Downside of this is tha

[Rails] Re: how do I get all records whose count of associations is above a certain number

2010-02-14 Thread Peter
And you can actually represent this in AR friendly syntax; no console in front of me but its something like Player.find(:select => "players.id", :joins => :trophies, :group => 'players.id', :having => 'count(players.id) > 5') Downside of this is that the returned objects in this case would only h

Re: [Rails] Re: how do I get all records whose count of associations is above a certain number

2010-02-14 Thread Frank Kim
Thanks everyone, that was really helpful! On Sun, Feb 14, 2010 at 4:31 AM, Frederick Cheung wrote: > > > On Feb 14, 6:54 am, Frank Kim wrote: >> I have a model, let's call it Player. >> >> It has many Trophies. >> >> How do I do a simple query in Rails in my controller that will return >> let's

[Rails] Re: how do I get all records whose count of associations is above a certain number

2010-02-14 Thread Frederick Cheung
On Feb 14, 6:54 am, Frank Kim wrote: > I have a model, let's call it Player. > > It has many Trophies. > > How do I do a simple query in Rails in my controller that will return > let's say all Players that have more than 5 tropies? > > Sorry for the dumb question but I can't figure it out. :-)

[Rails] Re: how do I get all records whose count of associations is above a certain number

2010-02-13 Thread Jarin Udom
The best way to do it is to set a counter_cache for the trophies. Example: class Trophy < ActiveRecord::Base belongs_to :player, :counter_cache => true You'll also have to create an integer column called trophies_count in the players table. If you already have data in there, you'll need to upda