diaspora/db/migrate/20110911213207_counter_cache_on_post_comments.rb
2011-12-16 20:39:24 -08:00

15 lines
404 B
Ruby

class CounterCacheOnPostComments < ActiveRecord::Migration
class Post < ActiveRecord::Base; end
def self.up
add_column :posts, :comments_count, :integer, :default => 0
execute <<SQL if Post.count > 0
UPDATE posts
SET comments_count = (SELECT COUNT(*) FROM comments WHERE comments.post_id = posts.id)
SQL
end
def self.down
remove_column :posts, :comments_count
end
end