Ensure counters only get updated if the record exists

This commit is contained in:
Karina Kwiatek 2022-02-04 22:08:25 +01:00 committed by Karina Kwiatek
parent e2462b1e51
commit 539505eccd
5 changed files with 10 additions and 10 deletions

View file

@ -28,13 +28,13 @@ class Answer < ApplicationRecord
end
end
self.user.decrement! :answered_count
self.question.decrement! :answer_count
self.user&.decrement! :answered_count
self.question&.decrement! :answer_count
self.smiles.each do |smile|
Notification.denotify self.user, smile
end
self.comments.each do |comment|
comment.user.decrement! :commented_count
comment.user&.decrement! :commented_count
Subscription.denotify comment, self
end
Notification.denotify self.question.user, self

View file

@ -10,8 +10,8 @@ class Appendable::Reaction < Appendable
before_destroy do
Notification.denotify parent.user, self
user.decrement! :smiled_count
parent.decrement! :smile_count
user&.decrement! :smiled_count
parent&.decrement! :smile_count
end
# rubocop:enable Rails/SkipsModelValidations

View file

@ -24,8 +24,8 @@ class Comment < ApplicationRecord
end
Subscription.denotify self, answer
user.decrement! :commented_count
answer.decrement! :comment_count
user&.decrement! :commented_count
answer&.decrement! :comment_count
end
def notification_type(*_args)

View file

@ -16,7 +16,7 @@ class Question < ApplicationRecord
end
end
user.decrement! :asked_count unless self.author_is_anonymous
user&.decrement! :asked_count unless self.author_is_anonymous
end
def can_be_removed?

View file

@ -45,8 +45,8 @@ class User < ApplicationRecord
has_many :subscriptions, dependent: :destroy_async
has_many :totp_recovery_codes, dependent: :destroy_async
has_one :profile, dependent: :destroy_async
has_one :theme, dependent: :destroy_async
has_one :profile, dependent: :destroy
has_one :theme, dependent: :destroy
has_many :bans, class_name: "UserBan", dependent: :destroy_async
has_many :banned_users, class_name: 'UserBan',