Skip to content

Instantly share code, notes, and snippets.

@obie
Created August 22, 2011 15:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obie/1162704 to your computer and use it in GitHub Desktop.
Save obie/1162704 to your computer and use it in GitHub Desktop.
adding a counter cache attribute to existing records
class AddCommentsCountToNominations < ActiveRecord::Migration
def self.up
add_column :nominations, :comments_count, :integer, null:false, default: 0
nomination_ids_with_comments = Set.new
Comment.all.each {|c| nomination_ids_with_comments << c.nomination_id}
nomination_ids_with_comments.each do |nomination_id|
Nomination.reset_counters(nomination_id, :comments)
end
end
def self.down
remove_column :nominations, :comments_count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment