How to pass arguments to functions in callbacks in model ?

I need to pass attributes to the function called in callback in rails model. For example,

after_save :update_something after_destroy :update_something, true #true is the argument that I need to pass

private def update_something(value = false) #… end

I found a way to do it like this

after_save :update_something after_destroy {|record| record.update_something(true)}

But in this case, I need to make the function update_something public, which I don’t want to do.

Any help ?

What about ...

def update_something(value=false)    # .. end

def after_save    update_something(true) end

Or use instance variable as conditional state instead of passing variable to a function called from macro (i.e after_save)

Arpit Jain wrote:

You can use ActiveRecord::Observer. For details please go to http://blog.invisible.ch/files/rails-reference-1.1.html

However theres another funny work around for this problem as your desired syntax is after_destroy :update_something, true

Just tweak little bit: after_destroy "update_something(true)"

declare the update_something method privately like: def update_something(param = value)    #operations based on #{param} end

I dont know your actual scenario. But i think * If u need to perform same action on :after_save and :after_destroy call-back then why do you need parameter * Secondly, if update_something performs differently based on the parameter then why don't you create two separate methods

Anyway looking for the real picture from you and hope to get the best architectural suggestion from anyone for this kind of problem :slight_smile:

Thank you Samiron Paul