From 8d2945e8a2227cc98b19f80222fd40cb40f33749 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 01:31:34 +0200 Subject: [PATCH] Fix answer attribute validation to check question's question_answers inclusion --- app/models/poll/answer.rb | 4 +--- app/models/poll/partial_result.rb | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index 4484060dd..ce4bc37af 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -9,9 +9,7 @@ class Poll::Answer < ActiveRecord::Base validates :author, presence: true validates :answer, presence: true - # temporary skipping validation, review when removing valid_answers - # validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }}, - # unless: ->(a) { a.question.blank? } + validates :answer, inclusion: { in: ->(a) { a.question.question_answers.pluck(:title) }} scope :by_author, ->(author_id) { where(author_id: author_id) } scope :by_question, ->(question_id) { where(question_id: question_id) } diff --git a/app/models/poll/partial_result.rb b/app/models/poll/partial_result.rb index 12b16aa3a..a44b61cad 100644 --- a/app/models/poll/partial_result.rb +++ b/app/models/poll/partial_result.rb @@ -10,7 +10,7 @@ class Poll::PartialResult < ActiveRecord::Base validates :question, presence: true validates :author, presence: true validates :answer, presence: true - validates :answer, inclusion: {in: ->(a) { a.question.valid_answers }} + validates :answer, inclusion: {in: ->(a) { a.question.question_answers.pluck(:title) }} validates :origin, inclusion: {in: VALID_ORIGINS} scope :by_author, ->(author_id) { where(author_id: author_id) }