[Ruby on Rails] About Active Record callbacks

What is a callback?

From the Rails guide

Callbacks are methods that are called at specific moments during the life cycle of an object. Callbacks allow you to write code that is always executed when an Active Record object is created / saved / updated / deleted / verified / loaded from the database, etc.

Active Record callbacks fire events before and after when an Active Record object such as Model is updated or deleted and its state changes. Any processing can be performed in the event.

How to register a callback

Example) Output a log when member data is deleted.

app/models/member.rb


class Member < ApplicationRecord
Abbreviation
  after_destroy do
    Rails.logger.info "Member is deleted: #{self.attributes}"
  end
end

When a member is deleted, the process in ʻafter_destroy` is executed.

type

Below are some examples of callbacks available in Active Record. before_ can define the processing before the trigger, around_ can define the processing before and after the trigger, and after_ can define the processing after the trigger. If it is before_validation, it occurs before the validation process performed by create and update.

Callback point create update destroy
before_validation ×
after_validation ×
before_save ×
around_save ×
after_save ×
before_create × ×
around_create × ×
after_create × ×
before_update ×
around_update ×
after_update ×
before_destroy × ×
around_destroy × ×
after_destroy × ×

Other examples of using

During development, I sometimes wanted to check the behavior when the deletion process failed, so I was able to make it fail by doing the following.

app/models/member.rb


class Member < ApplicationRecord
Abbreviation
  before_destroy { throw(:abort) }
end

Callback processing can be intentionally stopped by writing throw (: abort).

Things to watch out for in callbacks

--Since the callback is set in the model, the logic cannot be seen from the controller, and the developer may execute unexpected processing. --If you use a lot of callbacks or perform complicated processing, it becomes a fat model.

reference

--Rails Guide --Active Record Callbacks -Perfect Ruby on Rails [enhanced and revised edition]

Recommended Posts

[Ruby on Rails] About Active Record callbacks
Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
[Ruby on Rails] About bundler (for beginners)
[Rails] About active hash
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Ruby On Rails] About RubyAws :: Sigv4 :: Errors :: MissingCredentialsError
Portfolio creation Ruby on Rails
[Ruby on Rails] Delete s3 images with Active Strage
[Ruby on Rails] Debug (binding.pry)
Ruby on Rails config configuration
Ruby on Rails basic learning ①
Commentary on partial! --Ruby on Rails
Cancel Ruby on Rails migration
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
What is Rails Active Record?
Ruby on Rails record search, create if not find_or_create_by method
A note about the seed function of Ruby on Rails
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
About Rails 6
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
Ruby on Rails model creation / deletion command