Reputation: 56719
How can you separate callbacks so that after_create
runs one set of code, but !after_create
, so to speak, runs another?
Upvotes: 10
Views: 7110
Reputation: 12683
after_create
After new object created
after_update
After existing object updated
after_save
Both creation and update
Upvotes: 4
Reputation: 33171
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
Upvotes: 1
Reputation: 12868
after_create
callback for new object, after_update
for persisted one.
Upvotes: 27