This project is archived and is in readonly mode.

#2563 ✓resolved
Michael Siebert

[PATCH] add :update_only option to accepts_nested_attributes_for

Reported by Michael Siebert | April 25th, 2009 @ 10:54 AM | in 2.x

This option adds the ability for one-to-one associations to say "no i dont want the user to be able to create a new record just by leaving out the :id attribute", because at least in one of our applications, this would screw things up.


class Car < ActiveRecord::Base
  has_one :engine
  accepts_nested_attributes_for :engine, :update_only => true
end

class Engine < ActiveRecord::Base
  belongs_to :car
end

car = Car.create(:name => "Slow Car")

# this creates a new engine, since no engine exists
car.engine_attributes = {:ps => 40}
car.save!

# tunes the existing engine, doesn't create a new engine
car.engine_attributes = {:ps => 200}
car.save!

# does the same thing as above, :id is in fact ignored
car.engine_attributes = {:ps => 210, :id => car.engine.id}
car.save!

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages