On 27 July 2010 08:59, Mohammed Alenazi
<vb4max-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi
> I have two tables: users and tickets. A user has many tickets which is
> defined in the model. I want to order the users based on the number of
> tickets they have. What is the easiest way to do it in Rails without
> writing to many SQL statements.
>
Look at adding a "counter cache" column to your User model to store
the amount of tickets associated to them. Then you can just use that
column to sort by in a normal find.
http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.