Rails 3 - default_scope

I would like to order article tags on my index page by popularity, and not by creation date, i.e. tags with the highest number of articles in them from the highest to the lowest. My model is as follows:

class Tag < ActiveRecord::Base attr_accessible :name validates :name, :uniqueness => true # order by creation default_scope :order => 'created_at DESC' has_many :taggings, :dependent => :destroy has_many :articles, :through => :taggings end 
+4
source share
1 answer

I recommend using a column cache column to store taggings_count (which is automatically updated with new tags).

And then your default area might look like this:

 default_scope :order => 'taggings_count DESC' 

For more information, find "counter_cache" in the Rails Guide for AR Associations

+5
source

All Articles