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.