We have a Rails 6.1 application running an after_commit
callback to look into saved_changes
value to run some background workers. But the issue is saved_changes
returns empty if a model has accepts_nested_attributes_for
. It is not only after_commit
other callbacks like before_save
, and around_save
also return empty saved_changes
value.
For example.
on models/profile.rb
after_commit :run_worker, on: :update
belongs_to :user
accepts_nested_attributes_for :user
In this case saved_changes
is {}
but if I remove the accepts_nested_attributes_for
it works fine and gives me the changed value.
It is a known issue caused by double saves as mentioned on this GH issue. The workaround for 7.1 is adding config on the application.rb
config.active_record.run_commit_callbacks_on_first_saved_instances_in_transaction = true
This default is for Rails 7.1 and does not work for 6.1. Has someone else run through this issue and has a workaround?