ActiveRecord 2.3: Nested attribute changes disappear

Updated . Posted . Visible to the public.

There is a bug in ActiveRecord 2.3.x that leads to changes in nested forms getting lost.

class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks
end

If you access project.tasks after setting tasks through the nested attribute logic, all tasks will be reloaded and all changes will be lost. This usually happens

  • in validations
  • in callbacks
  • after validation errors, when rendering the view again

The attached initializer fixes those issues.

Tobias Kraze
Last edit
Keywords
parent, children, association
License
Source code in this card is licensed under the MIT License.
Posted by Tobias Kraze to makandra dev (2013-02-11 17:06)