Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

accepts_nested_attributes_for not updating attributes

#5815

In Rails 2.3.8 I discovered that accepts_nested_attributes_for may skip updating existing records under certain circumstances. I pulled my hair out yesterday because of this: In the application it didn't update my nested objects, reproducing this on the console updated the objects. It took me some hours to discover the difference (after finding many reports for similar bugs and workaround/solutions which didn't work for me):

In the application I did

class TestController < ApplicationController
  # class Container belongs_to :location; end
  # class Location has_many :containers; accepts_nested_attributes_for :containers, :allow_destroy => true; end

  def update
    @location = Location.find(params[:id])
    if @location.update_attributes params[:location]
      #...
    end
  end
end

This never worked, although the :containers_attributes hash was correctly passed in by the params hash. Only new records were added to the association. Existing records couldn't be updated or destroyed.

To investigate I copy/pasted the hash from the log to the console doing the following way:

$ script/console
> l = Location.find(123)
> l.containers.inspect   #<-- to see if the prerequisites for the following hash are met
> l.update_attributes(:containers_attributes => { ... })  #<-- copied from the development.log
> l.reload.containers.inspect   #<-- compare: it worked!

Now what was the difference? After a few hours of google'ing around it not easy to still have a view for that, but finally I discovered the small difference:

In my controller I didn't load the containers association prior to updating it while on the console I did. Adding :include => :containers to the Location finder fixed all my problems.

I think it is a bug in Rails that it does not implicitly do that if the association has not been loaded upon mass assignment to Location#containers_attributes=. It should be fixed.

Reported by Kai Krakow · October 15th, 2010 @ 12:11 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Kai Krakow
    Kai Krakow
    • Tag changed from nested attributes, assignment, associations to nested attributes, rails 2.3.8, assignment, associations, bugs

    October 15th, 2010 @ 12:13 PM

  2. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:32 AM

  3. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:47 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:47 PM

  5. bingbing