diff options
author | Raphael Sofaer <raphael@joindiaspora.com> | 2011-07-05 19:47:40 +0400 |
---|---|---|
committer | Raphael Sofaer <raphael@joindiaspora.com> | 2011-07-05 19:47:40 +0400 |
commit | 130914267bc6954a6735a33741e96c8d81e7f5cb (patch) | |
tree | 6b0f45c7d7f7cde1f1e95f46f64ce319b07fd3ae /db | |
parent | ecd79766ceb6508dc55cbc1255db8fc74172e5e2 (diff) |
Update the Post counter caches when adding the counter cache column
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20110705003445_counter_cache_on_post_likes.rb | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/db/migrate/20110705003445_counter_cache_on_post_likes.rb b/db/migrate/20110705003445_counter_cache_on_post_likes.rb index 2d4f55f4c..6e08f5577 100644 --- a/db/migrate/20110705003445_counter_cache_on_post_likes.rb +++ b/db/migrate/20110705003445_counter_cache_on_post_likes.rb @@ -1,6 +1,11 @@ class CounterCacheOnPostLikes < ActiveRecord::Migration + class Post < ActiveRecord::Base; end def self.up add_column :posts, :likes_count, :integer, :default => 0 + execute <<SQL if Post.count > 0 + UPDATE posts + SET posts.likes_count = (SELECT COUNT(*) FROM likes WHERE likes.post_id = posts.id) +SQL end def self.down |