Active Record Callbacks

Get it? Rails.

The Object Life Cycle

2 Ways to Implement

class CardsController < ApplicationControllerbefore_action :find_by_iddef update   puts @cardend
private
def find_by_id @card = Card.find_by_id(params[:id])endend
before_action :find_by_id, only: [:show, :index, :delete, :update]
class CardsController < ApplicationControllerbefore_action do   @card = Card.find_by_id(params[:id])enddef update    puts @cardendend

Callbacks Available to Use

Creation

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save
  • after_commit/after_rollback

Update

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save
  • after_commit/after_rollback

Destroy

  • before_destroy
  • around_destroy
  • after_destroy
  • after_commit/after_rollback

--

--

--

Software Developer — Game Creator — Let’s work together! Reach out on LinkedIn or Twitter @thedrewprint

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Zaio Rent-a-car Project: HTML and CSS

Seeing Beyond The Cells

Turn your Computer into a Gardener to grow Flowers and Trees with a Turtle and L-Systems (Part 2)

CS373 Spring 2022: Feb 21-Feb 27

Keycloak integrated into Otomi Container Platform

9 Most Inspiring Stories for Software Developers

Export Google Colab Notebook as HTML Webpage

An Engaging Experience with TIIDELab

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Richards

Andrew Richards

Software Developer — Game Creator — Let’s work together! Reach out on LinkedIn or Twitter @thedrewprint

More from Medium

Process incoming emails in Rails 6 with Action Mailbox : from development to production

Learning Ruby from scratch -part 01

“Hello, World” in Ruby

Ruby on Rails in a Nutshell