diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 00e4002c7..0f09378c5 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -331,7 +331,8 @@ class PostAction < ActiveRecord::Base end topic_id = Post.with_deleted.where(id: post_id).pluck(:topic_id).first - Topic.where(id: topic_id).update_all ["#{column} = ?", count] + topic_count = Post.where(topic_id: topic_id).sum(column) + Topic.where(id: topic_id).update_all ["#{column} = ?", topic_count] if PostActionType.notify_flag_type_ids.include?(post_action_type_id) PostAction.update_flagged_posts_count diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 4427be8f4..a7aacdd53 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -10,6 +10,7 @@ describe PostAction do let(:moderator) { Fabricate(:moderator) } let(:codinghorror) { Fabricate(:coding_horror) } let(:post) { Fabricate(:post) } + let(:second_post) { Fabricate(:post, topic_id: post.topic_id) } let(:bookmark) { PostAction.new(user_id: post.user_id, post_action_type_id: PostActionType.types[:bookmark] , post_id: post.id) } describe "messaging" do @@ -115,6 +116,17 @@ describe PostAction do end + describe "update_counters" do + + it "properly updates topic counters" do + PostAction.act(moderator, post, PostActionType.types[:like]) + PostAction.act(codinghorror, second_post, PostActionType.types[:like]) + post.topic.reload + post.topic.like_count.should == 2 + end + + end + describe "when a user bookmarks something" do it "increases the post's bookmark count when saved" do lambda { bookmark.save; post.reload }.should change(post, :bookmark_count).by(1)