diff --git a/app/models/article.rb b/app/models/article.rb index ad638dfc3..0d71784d3 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -375,8 +375,8 @@ class Article < ApplicationRecord end def update_score - new_score = reactions.sum(:points) + Reaction.where(reactable_id: user_id, reactable_type: "User").sum(:points) - update_columns(score: new_score, + self.score = reactions.sum(:points) + Reaction.where(reactable_id: user_id, reactable_type: "User").sum(:points) + update_columns(score: score, comment_score: comments.sum(:score), hotness_score: BlackBox.article_hotness_score(self), spaminess_rating: BlackBox.calculate_spaminess(self)) diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index a34216ef5..6121405af 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -1008,4 +1008,13 @@ RSpec.describe Article, type: :model do expect(article.plain_html).not_to include("highlight__panel") end end + + describe "#update_score" do + it "stably sets the correct blackbox values" do + create(:reaction, reactable: article, points: 1) + + article.update_score + expect { article.update_score }.not_to change { article.reload.hotness_score } + end + end end