Callbacks in Rails

Tram Ho

Learn about callbacks

  • Callback is a method in Activerecord, used when the object needs it.
  • Callback is often used to call and execute certain methods before and after the object has changes such as create, update, destroy, load data, validate, save, …
  • Since callbacks are methods that should be declared in the model before use, we will have two ways to declare callbacks:
  • Normal declaration

  • Declare type block

  • List of callbacks available in rails

Creating an Object

Updating an Object

Destroying an Object

To bypass the callback we can use the following

  • Use the skip_callback method
  • By the way, I talked more about the methods that can use callbacks in rails:

Also the methods that ignore callbacks are:

General In my opinion callbacks are an indispensable method when working with rails or any other language, through this article I hope to help you a little more in the process of learning callbacks, thank you for reading .
Share the news now

Source : Viblo