This project is archived and is in readonly mode.

#703 ✓invalid
Glenn Powell

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

Reported by Glenn Powell | July 25th, 2008 @ 02:55 PM | in 2.x

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.

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>

Pages