Don't save the answer if the voter is not recorded
Up until now, we were assuming the voter was valid, but were not raising an exception if it wasn't. And in the user interface everything seemed to be working properly. We were having this issue when skipping verification, when there could be voters without a document number, which would be considered invalid. Raising an exception when failing to save the voter and making sure the answer and the voter are saved inside a transaction solves the problem.
This commit is contained in:
@@ -14,7 +14,11 @@ class Poll::Answer < ApplicationRecord
|
||||
scope :by_author, ->(author_id) { where(author_id: author_id) }
|
||||
scope :by_question, ->(question_id) { where(question_id: question_id) }
|
||||
|
||||
def record_voter_participation(token)
|
||||
Poll::Voter.find_or_create_by(user: author, poll: poll, origin: "web", token: token)
|
||||
def save_and_record_voter_participation(token)
|
||||
transaction do
|
||||
touch if persisted?
|
||||
save!
|
||||
Poll::Voter.find_or_create_by!(user: author, poll: poll, origin: "web", token: token)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user