1

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.

4
  • Ok, the problem is in the build function because using new it works well. Commented May 25, 2011 at 11:02
  • Sorry, I do some changes to the code because the original code is quite different. Commented May 25, 2011 at 11:04
  • The method is actually 'accepts_nested_attributes_for'. Commented Aug 23, 2011 at 11:55
  • ...and belogns_to should obviously be belongs_to. (it might help other n00bs like me if you corrected the code) Commented Aug 23, 2011 at 12:19

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.