I have a module that gets included inside an Rails Observer.
The purpose is:
- Call on
after_save
andafter_update
a method namedcheck_integrity
- Call
check_integrity
at the end ofafter_save
or/andafter_update
if defined in the Observer.
So in short it should always call check_integrity
.
I tried something that looks like the following code:
module IntegrityObserver
extend ActiveSupport::Concern
included do
alias_method_chain :after_save, :check_integrity
alias_method_chain :after_update, :check_integrity
end
def check_integrity
# do something
end
end
class UserObserver < ActiveRecord::Observer
include IntegrityObserver
def after_save(object)
# do something
end
end
But it raise an error: activesupport-3.0.17/lib/active_support/core_ext/module/aliasing.rb:31:in alias_method': undefined method after_update' for class TaskObserver' (NameError)
Someone has any idea how I can do what I want?
Thanks!
ActiveRecord already provides observer functionality to monitor the lifecycle of your models. Here is how you can register a generic observer which responds to more than one model:
In your config/application.rb file:
Check out more examples here.