diff --git a/app/models/comment.rb b/app/models/comment.rb
index dca894b7c..b81712a2d 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -9,7 +9,7 @@ class Comment < ActiveRecord::Base
validates :body, presence: true
validates :user, presence: true
- belongs_to :commentable, polymorphic: true
+ belongs_to :commentable, polymorphic: true, counter_cache: true
belongs_to :user, -> { with_hidden }
has_many :inappropiate_flags, :as => :flaggable
@@ -88,4 +88,8 @@ class Comment < ActiveRecord::Base
children.count
end
+ def after_hide
+ commentable_type.constantize.reset_counters(commentable_id, :comment_threads)
+ end
+
end
diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb
index 4dd866df2..2d1e2deb4 100644
--- a/app/views/debates/_debate.html.erb
+++ b/app/views/debates/_debate.html.erb
@@ -9,7 +9,7 @@
<%= link_to debate.title, debate %>
- <%= link_to t("debates.debate.comments", count: debate.comment_threads.count), debate_path(debate, anchor: "comments") %>
+ <%= link_to t("debates.debate.comments", count: debate.comments_count), debate_path(debate, anchor: "comments") %>
<%= link_to debate.description, debate %>
diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb
index 3b91edc8d..af25f28b1 100644
--- a/app/views/debates/show.html.erb
+++ b/app/views/debates/show.html.erb
@@ -40,7 +40,7 @@
<%= l @debate.created_at.to_date %>
•
- <%= link_to t("debates.show.comments", count: @debate.comment_threads.count), "#comments" %>
+ <%= link_to t("debates.show.comments", count: @debate.comments_count), "#comments" %>
•
<%= render 'debates/flag_as_inappropiate_actions', debate: @debate %>
@@ -79,7 +79,7 @@
<%= t("debates.show.comments_title") %>
- (<%= @debate.comment_threads.count %>)
+ (<%= @debate.comments_count %>)
<% if user_signed_in? %>
<%= render 'comments/form', {parent: @debate, toggeable: false} %>
diff --git a/app/views/welcome/_featured_debate.html.erb b/app/views/welcome/_featured_debate.html.erb
index 11cb3b733..7ad53608b 100644
--- a/app/views/welcome/_featured_debate.html.erb
+++ b/app/views/welcome/_featured_debate.html.erb
@@ -8,7 +8,7 @@
<%= link_to featured_debate.title, featured_debate %>
- <%= link_to t("debates.show.comments", count: featured_debate.comment_threads.count), debate_path(featured_debate, anchor: "comments") %>
+ <%= link_to t("debates.show.comments", count: featured_debate.comments_count), debate_path(featured_debate, anchor: "comments") %>
<%= link_to featured_debate.description, featured_debate %>
diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb
index 98420c000..4115fedcf 100644
--- a/spec/models/comment_spec.rb
+++ b/spec/models/comment_spec.rb
@@ -8,6 +8,15 @@ describe Comment do
expect(comment).to be_valid
end
+ it "should update cache_counter in debate after hide" do
+ debate = create(:debate)
+ comment = create(:comment, commentable: debate)
+
+ expect(debate.reload.comments_count).to eq(1)
+ comment.hide
+ expect(debate.reload.comments_count).to eq(0)
+ end
+
describe "#children_count" do
let(:comment) { create(:comment) }
let(:debate) { comment.debate }