Skip to content

Instantly share code, notes, and snippets.

@valachi
Created April 1, 2013 15:08
Show Gist options
  • Save valachi/5285456 to your computer and use it in GitHub Desktop.
Save valachi/5285456 to your computer and use it in GitHub Desktop.
class AddCommentsCountToArticles < ActiveRecord::Migration
def up
add_column :articles, :comments_count, :integer, null: false, default: 0
Article.reset_column_information
ids = Set.new
Article.all.each {|art| ids << art.id}
ids.each do |article_id|
Article.reset_counters(article_id, :comments)
end
end
def down
remove_column :articles, :comments_count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment