diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index 8e02dcfe2..a46f28a8c 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -20,6 +20,7 @@ class Poll::Question < ActiveRecord::Base validates :title, presence: true validates :author, presence: true + validates :poll_id, presence: true validates :title, length: { minimum: 4 } validates :description, length: { maximum: Poll::Question.description_max_length } diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb index ee90d2ada..13ec0db58 100644 --- a/spec/features/admin/poll/polls_spec.rb +++ b/spec/features/admin/poll/polls_spec.rb @@ -183,7 +183,7 @@ feature 'Admin polls' do scenario 'Add question to poll', :js do poll = create(:poll) - question = create(:poll_question, poll: nil, title: 'Should we rebuild the city?') + question = create(:poll_question, title: 'Should we rebuild the city?') visit admin_poll_path(poll) diff --git a/spec/models/poll/question_spec.rb b/spec/models/poll/question_spec.rb index 057dfebca..db6a0f762 100644 --- a/spec/models/poll/question_spec.rb +++ b/spec/models/poll/question_spec.rb @@ -9,6 +9,20 @@ RSpec.describe Poll::Question, type: :model do end end + describe "#poll_question_id" do + it "should be invalid if a poll is not selected" do + q = create(:poll_question) + q.poll_id = nil + expect(q).to_not be_valid + end + + it "should be valid if a poll is selected" do + q = create(:poll_question) + q.poll_id = 1 + expect(q).to be_valid + end + end + describe "#copy_attributes_from_proposal" do it "copies the attributes from the proposal" do create_list(:geozone, 3)