Sorting with related tables

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.

On 27 July 2010 08:59, Mohammed A. [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs