I have an after save callback:
after_save :do_an_expensive_task_on_basis_of_flag_value
I later realized that this is a very expensive task and it does not make sense to call it when the value of flag is not set(on create) OR value of flag is not updated on update.
So one thing , i can do is to replace it with:
after_create :do_an_expensive_task_on_basis_of_flag_value , :if => Proc.new {|a| a.flag.present?}
after_update :do_an_expensive_task_on_basis_of_flag_value , :if => Proc.new {|a| a.flag_changed?}
But i am looking for a one liner, with a proc such that this task is called only when Flag is set on create and flag is updated on update with :after_save
.
Note: This is not correct way:
after_save :do_an_expensive_task_on_basis_of_flag_value , :if => Proc.new {|a| a.flag.present? or a.flag_changed?}
As it still calls callback on update , even when flag is not changed.