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.

Mass-assign > update_attributes not handling Hash with :id of existing nested models

#703

I am attempting to utilize mass-assign for my models, but the system seems to break down when I use an association_collection of nested models and try to edit/update the parent model.

My create methods work fine, as the value passes into the parent's attributes is an Array of nested model attributes:

contact => { emails => [ { :address => "foo@bar.com" }, { :address => "bar@foo.com" } ] }

But, when I try to edit/update my parent model, the form's field names for the nested models include the :id of those models (which I believe is correct, as you are attempting to update those particular nested models). However, this causes the resulting value passed to the parent's update_attributes to no longer be an Array, but a Hash of this form:

contact => { emails => {"5" => { :address => "oof@rab.com" }, "6" => { :address => "rab@oof.com" } } }

Where the 5 and 6 are the :id values of the respective emails. This gets passed into the update_attributes, which eventually gets to:

AssociationCollection.replace(other_array)

which assumes "emails" to be an Array instead of a Hash.

Shouldn't this replace method be "smarter" so that if a Hash is passed in, it will ascertain the :id values from it, and then reassign the model attributes accordingly?

Furthermore, what would happen if there were a combination of updated (existing) nested models and new nested models (with no :id in the field parameter). I would hope that the mass-assign functionality of update_attributes handles this situation as well.

Reported by Glenn Powell · July 25th, 2008 @ 02:55 PM

State: invalid
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Clemens Kofler
    Clemens Kofler

    IIRC, parts of what you mention here have already made it into the current edge. A sensible implementation is at the moment being discussed at the Rails Core mailing list in this topic.

    July 25th, 2008 @ 04:46 PM

  2. Tarmo Tänav
    Tarmo Tänav
    • State changed from new to invalid
    • Tag changed from activerecord, bug, has_many to activerecord, bug, has_many

    The ideal approach to this is currently being discussed in rails-core, but closing this ticket for now.

    September 18th, 2008 @ 06:24 AM