diaspora/db/migrate/20110911213207_counter_cache_on_post_comments.rb.rb
2011-09-12 16:29:39 +02:00

14 lines
403 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