views:

54

answers:

1

I am using ruby on rails 2.3.5 .

I have user instance. On run time depending on certain conditions I want to add an after_save callback just for this instance.

The other issue is that this after_save should take a block or a proc as parameter.

What I want is something like this. This is psuedo code.

user = User.first

proc = Proc.new do puts 'this is foo' end

user.after_save proc

A: 

This is how I would do it. In the model I would have:

attr_accessor :after_save_callback

after_save { |record|
  record.after_save_callback.call unless record.after_save_callback.nil?
}

Whereas binding the event can be something like:

user = User.first
user.after_save_callback = Proc.new { "actual code" }
vise
thanks but I am looking for a more transparent solution. Model class can't be changed.
Nadal