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
-
Clemens Kofler July 25th, 2008 @ 04:46 PM
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.
-
Tarmo Tänav September 18th, 2008 @ 06:24 AM
- → 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.
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
The Git repository resides at http://github.com/rails
Check out the current development trunk (Edge Rails) with:
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".
