Callbacks are methods which are triggered in an objects lifecycle. Examples of callback for an Active Record object include before_save, after_save, before_create, and after_destroy. Callbacks are able to execute code in response to events.


class User < ApplicationRecord
  before_save :send_welcome_email

  def send_welcome_email
    puts "Email sent"
# rails console
"Email sent"

Above is an example of implementing the callback as a method (send_welcome_email) and then registering the method as a callback using a class method before_save. Methods that are used as callbacks should generally be private methods so that they can only be called from within the context of the current object

See Also

Private Methods