I have the following snippet:
class Product
after_commit :do_something, on: %i(update create)
def do_something
if # update
...
else # create
...
end
end
end
How to know what event triggered the after commit here?
Please don't tell me to have 2 after commits like:
after_commit :do_something_on_update, on: :update
after_commit :do_something_on_create, on: :create
ActiveRecord uses transaction_include_any_action?
:
def do_something
if transaction_include_any_action?([:create])
# handle create
end
if transaction_include_any_action?([:update])
# handle update
end
end
A transaction can include multiple actions. If both :create
and :update
are possible in the same transaction in your program you need two if
s, not an if
/else
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With