Fix exception creating an answer without an author

We were getting `undefined method `lock!' for nil:NilClass` when the
question allowed multiple answers.
This commit is contained in:
Javi Martín
2024-05-10 00:56:05 +02:00
parent bcaf05f78f
commit 0c650c423d
2 changed files with 8 additions and 1 deletions

View File

@@ -36,7 +36,7 @@ class Poll::Answer < ApplicationRecord
private
def max_votes
return if !question || question&.unique? || persisted?
return if !question || !author || question&.unique? || persisted?
author.lock!

View File

@@ -18,6 +18,13 @@ describe Poll::Answer do
expect(answer).not_to be_valid
end
it "is not valid without an author when multiple answers are allowed" do
answer.author = nil
answer.question = create(:poll_question_multiple)
expect(answer).not_to be_valid
end
it "is not valid without an answer" do
answer.answer = nil
expect(answer).not_to be_valid