开发者

Rails 3.1.rc1 and accept_nested_attributes_for

开发者 https://www.devze.com 2023-03-08 13:31 出处:网络
I have the following models: class Survey < ActiveRecord::Base set_primary_key :survey_id # I\'m using external DB

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_attri开发者_如何学Cbutes_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.


It was fixed in this commit.

The fix is present Rails 3.1.0rc2, so if you update your Rails version in your Gemfile:

gem 'rails', '3.1.0.rc2'

And run

$ bundle update rails

It should work as expected.

0

精彩评论

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