Use a transaction when registering a vote

Otherwise it would be possible to increment the votes counter without
actually introducing the vote.
This commit is contained in:
Javi Martín
2023-07-01 22:43:05 +02:00
parent 9491b20314
commit 1d7a269408

View File

@@ -107,8 +107,13 @@ class Debate < ApplicationRecord
def register_vote(user, vote_value)
if votable_by?(user)
Debate.increment_counter(:cached_anonymous_votes_total, id) if user.unverified? && !user.voted_for?(self)
vote_by(voter: user, vote: vote_value)
transaction do
if user.unverified? && !user.voted_for?(self)
Debate.increment_counter(:cached_anonymous_votes_total, id)
end
vote_by(voter: user, vote: vote_value)
end
end
end