rails/rails — Issue #4200: It's impossible to add a custom callback to AR class
Author: romanbsd
Created: 2011-12-27 08:39:03+00:00
State: open

Consider the following:

```ruby
class Post
define_model_callbacks :approve, :only => :after
end

class PostObserver
def after_approve(post)
# do something
end
end
```

This won't work as expected, because ActiveRecord::Observer defines the notify methods using the following code:

```ruby
def define_callbacks(klass)
observer = self
observer_name = observer.class.name.underscore.gsub('/', '__')

ActiveRecord::Callbacks::CALLBACKS.each do |callback|
next unless respond_to?(callback)
callback_meth = :"_notify_#{observer_name}_for_#{callback}"
unless klass.respond_to?(callback_meth)
klass.send(:define_method, callback_meth) do |&block|
observer.update(callback, self, &block)
end
klass.send(callback, callback_meth)
end
end
end
```

Therefore only ActiveRecord::Callbacks::CALLBACKS are going to be used.