On Feb 14, 6:54 am, 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. :-)


If for some reason you don't want to use a counter cache then you can
write it as

SELECT players.*, count(*) as trophy_count from players
inner join trophies on trophies.player_id = players.id
group by players.id having trophy_count > 5

But using a counter_cache and an index on that column would be way
faster.

Fred

-- 
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.

Reply via email to