Rails 3.1.rc1和accept_nested_attributes_for

时间:2011-05-24 16:48:38

标签: ruby-on-rails-3 nested-attributes

我有以下型号:

class Survey < ActiveRecord::Base
  set_primary_key :survey_id # I'm using external DB
  belongs_to :user #UPDATED
  has_many :questions, :dependent => :destroy
  accept_nested_attributes_for :questions
end

class Question < ActiveRecord::Base
  set_primary_key :question_id # I'm using external DB
  belogns_to :survey
end

如果我去rails控制台并保存模型:

>> params = {"title"=>"Survey 1", "questions_attributes"=>{"0"=>{"title"=>"Question 2"}}}
>> survey = User.first.surveys.build(params) #UPDATED
>> survey.questions.size
=> 2
>> survey = User.first.surveys.new(params)
>> survey.questions.size
=> 1

Rails正在重复调查问题资源。也许这是一个Rails 3.1错误?该代码类似于railscasts第197集。

1 个答案:

答案 0 :(得分:4)

已在this commit.

中修复

修复程序存在Rails 3.1.0rc2,所以如果你在Gemfile中更新你的Rails版本:

gem 'rails', '3.1.0.rc2'

然后运行

$ bundle update rails

它应该按预期工作。