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.
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.