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
end

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

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

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 »

Tickets have moved to Github

The new ticket tracker is available at https://github.com/rails/rails/issues

Shared Ticket Bins

Attachments

Referenced by

Pages