Shortcut Notation in Rails Callbacks

As I have spent the last few weeks/months learning Ruby and Rails, I have grown to love it more and more. The ease of use and the high learning curve make is fantastic for people like me who aren’t developers by nature.

I have recently come across a scenario in Rails that there is not yet a shortcut for. If you have a method that you want to execute in a few different callbacks, there is no shortcut for it. ie, I have the following:

  after_create   :methodFoo
  after_save     :methodFoo
  after_destroy  :methodFoo

I would like to be able to write this as:

 after_create, after_save, after_destroy => :methodFoo

This would obviously only work if the callbacks were then treated as arrays of functions and each time a callback was found in the model, the method was pushed onto the array.

Posted in Rails. Tags: . No Comments »