Ram Laxman Yadav bio photo

Ram Laxman Yadav

Ruby on Rails Developer

Email Twitter LinkedIn Github Stackoverflow

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

Models

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
  
  private
  def do_something(params = nil)
  puts "params"
  end
end

# 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

Thanks!!