In the OpenProject application we have two models:
class CustomField < ActiveRecord::Base
has_many :custom_options, -> { order(position: :asc) }, dependent: :delete_all
accepts_nested_attributes_for :custom_options
...
end
class CustomOption < ActiveRecord::Base
belongs_to :custom_field, touch: true
...
end
Then in the custom field controller we modify the options of a custom field via mass assignment and save the record:
@custom_field.attributes = get_custom_field_params
if @custom_field.save
...
Because of touch: true
being configured on the custom_field
association in CustomOption
I would have expected the custom field's updated_at
attribute to be updated at this point which does not happen however. The log does not show any SQL requests similar to UPDATE custom_fields SET update_at = ....
. The changes to the custom options themselves, regardless of whether it is adding a custom option or modifying one, are correctly persisted.
Debugging showed:
- The same erroneous behaviour when using
custom_field.custom_options.build
and a subsequentcustom_field.save
- The desired behaviour when using
custom_field.custom_options.create
- The desired behaviour when using
CustomOption.create custom_field: x
- The desired behaviour when using
custom_field.custom_option[x].destroy
I know I could work around the problem by defining after_save
callbacks, but this behaviour really irks me as it was unexpected.
Is the behaviour as outlined above desired and documented? If so, where can I find this information?
after_save
callbacks it is, then. I actually had the same suspicion but only deactivated theacts_as_list
on the child (CustomOption
). – Leafy