How can you separate callbacks so that after_create
runs one set of code, but !after_create
, so to speak, runs another?
after_create
After new object created
after_update
After existing object updated
after_save
Both creation and update
after_create
callback for new object, after_update
for persisted one.
You can have multiple callbacks which only execute based on conditions
model.rb
after_create :only_do_if_this
after_create :only_do_if_that
def only_do_if_this
if do_this?
# code...
end
end
def only_do_if_that
if do_that?
# code...
end
end
You can also add the condition to the callback itself
after_create :only_do_if_this, :if => proc { |m| m.do_this? }
after_create :only_do_if_that, :if => proc { |m| m.do_that? }
def only_do_if_this
# code ...
end
def only_do_if_that
# code...
end
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