Question

I have the two models:

class Question < ActiveRecord::Base
  has_many :answers, :dependent => :destroy
  accepts_nested_attributes_for :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
end

And the below controller:

class QuestionsController < ApplicationController

  def new
    @question = Question.new  
    4.times { @question.answers.build }
  end

end

When i try to load the new.html.haml view I'm getting this error:

Unknown attribute: question_id

The view look like below:

= simple_form_for @question do |f|
  = f.input :title
  = f.simple_fields_for :answers do |a|
    = a.input :text
  = f.submit
Was it helpful?

Solution

It seems like you forgot to create question_id field in Answers table.

There is 1-M relationship between your two models Question and Answer. Make sure you have foreign_key question_id present in Answers table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top