Nested attribute update_attributes uses insert rather than update Nested attribute update_attributes uses insert rather than update ruby-on-rails ruby-on-rails

Nested attribute update_attributes uses insert rather than update


For everyone who has the same problem in Rails 4: fields_for already adds the id for your nested forms but you have to permit the :id parameter. I only permitted an :object_name_id parameter and since this does not throw any errors it took me some time until i saw this in the server logs. Hopefully this helps someone wasting less time than me on this :)


If you check your form, you need to set the id attribute within the nested attribute hash for your Profile object. If the id is not set, ActiveRecord assumes it's a new object.

For example, if you had an ERB form building a set of 'user' parameters with a nested 'profile_attributes' parameter hash for the nested profile within the user, you could include a hidden value for the profile id, like this:

<%= hidden_field "user[profile_attributes][id]", @profile.id %>


I solved this problem by adding the update_only option:

accepts_nested_attributes_for :profile, update_only: true

Now a new profile is only created if one does not already exist.