Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Section A.2. Callbacks

A.2. Callbacks

Callbacks gives any class Active Record–style callbacks. It is used by defining the callbacks that the model will use, then in your model running the callbacks at the appropriate time. Once defined you have access to before, after, and around custom methods.

class Record
  extend ActiveModel::Callbacks

  define_model_callbacks :create
  define_model_callbacks :update, :destroy, :only => :before

  before_update :my_callback

  def save
    _run_update_callbacks do
      # Your save code here
    end
  end

  private
  def my_callback
    # Your callback code here
  end
end


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial