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
update the counters after creating the column:
def self.up
add_column :players, :trophies_count, :integer, :default => 0
Player.reset_column_information
Player.all.each do |p|
Player.update_counters p.id, :trophies_count =>
p.trophies.length
end
end
Then you can just do:
Player.all(:conditions => ["trophies_count > ?", 5])
Jarin Udom
Robot Mode LLC
On Feb 13, 10:54 pm, Frank Kim <[email protected]> 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 Kimhttp://betweengo.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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.