Search code examples
ruby-on-railssaveruby-on-rails-2

Rails 2 create method and saving parent


I'm upgrading a Rails 1 app to 2.3, and I'm running into a problem with a code block like the following:

@foo = Foo.new(...)

params[:baz].each do |b|
  @foo.bars.create(...)
end

...

@foo.save

This worked fine in Rails 1. After the upgrade, invoking this create method yields:

ActiveRecord::RecordNotSaved in BazController#create :
You cannot call create unless the parent is saved

How did the create method change between Rails 1 and 2, and what's the proper way to handle this now?


Solution

  • Use @foo.bars.build instead of create