-
Type: Improvement
-
Resolution: Duplicate
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
Problem Statement
Based on this example from Rails: https://guides.rubyonrails.org/active_record_callbacks.html#transaction-callbacks
class Picture < ApplicationRecord after_commit :delete_picture_file_from_disk, on: :destroy def delete_picture_file_from_disk File.delete(filepath) if File.exist?(filepath) end end
This callback will only be triggered if the Picture object is deleted within a transaction.
If you add an additional after_destroy callback, then destroying within a transaction will trigger the delete_picture_file_from_disk method twice: once when destroy is called, and once again on commit. A further problem is that, if the transaction is rolled back, then the after_destroy would have still been called.
What makes the most sense for users is to have a callback method that is guaranteed to trigger only once immediately after the persistence action has happened, either on commit (if transaction) or else after a normal op (if non-transaction).
Proposed Solutions
Possible solutions could be:
1. Introduce a new after_persistence callback, which has the behavior described above.
2. Alter the behavior of the existing callbacks after_commit and/or after_save. This is probably undesirable as it would create divergence from Rails.
- duplicates
-
MONGOID-5531 Add after_commit and after_rollback callbacks
- Closed
- has to be done before
-
MONGOID-5540 Add after_*_commit callbacks
- Backlog