This project is archived and is in readonly mode.

#228 ✓wontfix
Damian Janowski

Add add_counter_cache_column to ActiveRecord migrations

Reported by Damian Janowski | July 17th, 2008 @ 01:52 AM

This method makes it straightforward to add new columns to work with :counter_cache on ActiveRecord models. Apart from adding the column (default 0), it will update the counters for all the records.

For example:

def self.up
  add_counter_cache_column :posts, :comments

Creates a new comments_count column on posts and updates all current posts with the comments count -- equivalent to:

Post.find(:all).each do |post|
  Post.update_counters, :comments_count => post.comments.count

Updated docs and test included.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

Shared Ticket Bins


Referenced by