1

I'm sitting here now for 8 hours to figure out how that works: I'm trying to modify the example in http://asciicasts.com/episodes/196-nested-model-form-part-1 into a one-to-one relationship.

class Survey < ActiveRecord::Base
  has_one :question, :dependent => :destroy
  accepts_nested_attributes_for :question
end

class Question < ActiveRecord::Base
  belongs_to :survey
end

Controller:

def new  
  @survey = Survey.new  
  @survey.questions.build  
end  

It works great if I use a one-to-many relationship like:

class Survey < ActiveRecord::Base
  has_many :questions, :dependent => :destroy
  accepts_nested_attributes_for :questions
end

What am I doing wrong?

1
  • What's failing and where? Can you post your view code or some errors? Commented Aug 30, 2011 at 20:46

1 Answer 1

2

Try @survey.build_question instead of @survey.questions.build.

I think that is the right way to build questions when you use a one-to-one relationship.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.