Rails 3.1.rc1 和accept_nested_attributes_for
我有以下模型:
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 集。
I have the following models:
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
If I go to rails console and save a model:
>> 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 is duplicating question resource on surveys. Maybe is it a Rails 3.1 bug? The code is similiar to railscasts episode 197.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它已在此提交中修复。
修复存在于Rails 3.1.0rc2中,所以如果在 Gemfile 中更新 Rails 版本:
并运行
它应该按预期工作。
It was fixed in this commit.
The fix is present Rails 3.1.0rc2, so if you update your Rails version in your Gemfile:
And run
It should work as expected.