开发者

nested attributes with one-to-one relationship

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜