Counter Cache in rails 2.1
Counter cache is really an important feature in rails 2.1 . This method is really helpful when u want to keep a count on something and avoid firing sql query every time which results in more processing times.
Here it goes
To activate this feature, you need to take two simple steps. First, add the
option :counter_cache to the belongs_to declaration in the child table.
class LineItem < counter_cache =””> true
Second, in the definition of the parent table (products in this example) you
need to add an integer column whose name is the name of the child table with
create_table :products, :force => true do |t|
t.column :title, :string
t.column :description, :text
t.column :line_items_count, :integer, :default => 0
Thats it .. Counter cache is enabled now.