How to pass parameters in rails callback | Ram Laxman Yadav

How to pass parameters in rails callback

Hello There, Today I will discuss how to pass parameters in active record callback methods:


In models write callback method in following style like

class Book < ApplicationRecord
  #==== Callbacks ==========================================================
  after_commit :do_something, on: :create
  after_commit -> { do_something('I am parameterize call') }, on: :destroy
  def do_something(params = nil)
  puts "params"

# here you can see there is a method named do_something is called with parameters.
# if you delete a book then the output will be like:
# I am parameterize call


Ram Laxman Yadav
Ram Laxman Yadav
Ruby on Rails Developer