This project is archived and is in readonly mode.

#5815 ✓stale
Kai Krakow

accepts_nested_attributes_for not updating attributes

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

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.

Comments and changes to this ticket

  • Kai Krakow

    Kai Krakow October 15th, 2010 @ 12:13 PM

    • Tag changed from nested attributes, assignment, associations to nested attributes, rails 2.3.8, assignment, associations, bugs
  • Ryan Bigg

    Ryan Bigg October 19th, 2010 @ 08:32 AM

    • Tag cleared.
    • Importance changed from “” to “Low”

    Automatic cleanup of spam.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:47 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:47 PM

    • State changed from “open” to “stale”
  • bingbing

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>

Pages