How to pass parameters in rails callback

Posted by : on

Category : Rails

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!!



About Ram Laxman Yadav
Ram Laxman Yadav

Senior Software Engineering Professional | Tech Enthusiast | Mentor | Payments | Hospitality | E-Commerce, based in NCR, India

Email : info@ramlaxman.co.in

Website : https://ramlaxman.co.in