Rails 4: add exemption for after_update in model
-
20-12-2019 - |
Question
I have an after_update callback in my model
after_update :do_something
However, I don't want the method to be called after an object has been created. Is is possibe to add an exemption to after_update?
Solution
Rails has many different callbacks. When you create an object after_create
and after_save
are called. When you update an existing object, after_update
and after_save
are called.
after_create
is used only on creation as after_update
is only for updating existing objects. after_save
runs on both cases, after the proper create/update callback.
If you don't want the method to be called only after an object has been created you should use after_create
instead.
You could define as many callbacks as you need
class User < ActiveRecord::Base
after_create :one_method
after_create :another_method
after_update :one_more_method_only_for_updates
def one_method
end
def another_method
end
def one_more_method_only_for_updates
end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow