Richard Schneeman wrote:
> I have a model_one that :has_many model_two-s, I would like to find the
> ten model_one's that have the most model_two's. What is the best way to
> do this?

Something like this?

Model_One.find(:all, :limit => 10, :joins => :model_two,
     :order => "count(model_twos.model_one_id) desc",
     :group => "model_ones.id")

I could do it in raw SQL, but I don't know the exact match. Fred Cheung or 
someone will correct me if I got it wrong!

The :joins is so you don't eager-load the models and interfere with the group 
concept, I suspect. The group is so you are allowed to count within each group.


--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to