From 0c650c423dc649d1c86587300b403e8bf39e88c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 10 May 2024 00:56:05 +0200 Subject: [PATCH] Fix exception creating an answer without an author We were getting `undefined method `lock!' for nil:NilClass` when the question allowed multiple answers. --- app/models/poll/answer.rb | 2 +- spec/models/poll/answer_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index 41d76e925..ad1827e59 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -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! diff --git a/spec/models/poll/answer_spec.rb b/spec/models/poll/answer_spec.rb index fd77d5666..dc3dee8f5 100644 --- a/spec/models/poll/answer_spec.rb +++ b/spec/models/poll/answer_spec.rb @@ -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