Sorting with related tables

0 views
Skip to first unread message

Mohammed Alenazi

unread,
Jul 27, 2010, 3:59:38 AM7/27/10
to Ruby on Rails: Talk
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.

Michael Pavling

unread,
Jul 27, 2010, 11:51:38 AM7/27/10
to rubyonra...@googlegroups.com

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

Reply all
Reply to author
Forward
0 new messages