Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: add exemption for after_update in model

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?

like image 505
Albert Avatar asked Nov 29 '25 10:11

Albert


1 Answers

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
like image 69
wacko Avatar answered Dec 01 '25 00:12

wacko