Closed nin9 closed 2 years ago
Got the same bug after upgraded to rails 6.1 & paranoia 2.4.3. I figured out a workaround by overwriting paranoia_destroy
:
# config/initializers/fix_paranoia.rb
module Paranoia
# override method in paranoia
def paranoia_destroy
transaction do
run_callbacks(:destroy) do
@_disable_counter_cache = deleted?
result = paranoia_delete
next result unless result && ActiveRecord::VERSION::STRING >= '4.2'
each_counter_cached_associations do |association|
foreign_key = association.reflection.foreign_key.to_sym
next if destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key
next unless send(association.reflection.name)
association.decrement_counters
end
@_trigger_destroy_callback = true
@_disable_counter_cache = false
# Modified line
# fix the after_commit callback bug
# https://github.com/rubysherpas/paranoia/issues/511
@_new_record_before_last_commit = false
result
end
end
end
alias_method :destroy, :paranoia_destroy
end
@nin9 @zhengpd I also made a PR for this: #513
fix will be released as v2.5.4 later
released as v2.6.0
When a record is created then destroyed, after_commit on create callbacks are called.
Steps to reproduce
Run the following ruby script:
Note that if you called
transaction_include_any_action?([:create])
inside the callback function it will return true which is wrong since the action is destroy not create.