railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.87k stars 2.25k forks source link

Stop using update_only to decide to show subform on create #3649

Closed myers closed 8 months ago

myers commented 9 months ago

Closes #2626

There seems to be some confusion here about what :update_only means in accepts_nested_attributes_for in Rails Admin. It is currently use to decide to show the subform for a child record when creating a new record. I don't think this is correct.

https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

For a one-to-one association, this option allows you to specify how nested attributes are going to be used when an associated record already exists. In general, an existing record may either be updated with the new set of attribute values or be replaced by a wholly new record containing those values. By default the :update_only option is false and the nested attributes are used to update the existing record only if they include the record’s :id value. Otherwise a new record will be instantiated and used to replace the existing one. However if the :update_only option is true, the nested attributes are used to update the record’s attributes always, regardless of whether the :id is present. The option is ignored for collection associations.

This PR no longer attempts to use update_only, and only looks at field.inline_add.

Also as a bonus removing an unless with || makes my Grug brain happy.

mshibuya commented 8 months ago

Totally makes sense, thank you for the PR 👍