This project is archived and is in readonly mode.
ActiveRecord::Observer is not aware of method added by including modules
Reported by Robert Pankowecki | November 25th, 2010 @ 05:01 PM
This implementation works"
class RequestObserver < ActiveRecord::Observer
def after_commit(o)
# do something
end
end
however this one not:
class RequestObserver < ActiveRecord::Observer
module A
def after_commit(o)
# do sth
end
end
include A
end
You need to do it this way:
class RequestObserver < ActiveRecord::Observer
module A
def after_commit(o)
# do sth
end
end
include A
self.observed_methods += [:after_commit]
self.observed_methods.freeze
end
This is because of current implementation of the observer class:
def self.method_added(method)
method = method.to_sym
if ActiveRecord::Callbacks::CALLBACKS.include?(method)
self.observed_methods += [method]
self.observed_methods.freeze
end
end
It is not mentioned in the documentation, nor intuitive.
Couldn't we just check respond_to?
result to know
what the observer is capable of instead of keeping the list in
observed_methods field?
I can understand that adding new methods to the observer later dynamically is not supported because of performance reason but not supporting primary Ruby construction is a shame.
BTW: Here is my concept: let ActiveRecord::Observer have
included an module with all callbacks methods defined as empty
methods
We wouldn't have to worry about the methods existence and could
just call them. Writing a method in the observer or including
another module would overwrite them.
Comments and changes to this ticket
-
Neeraj Singh November 25th, 2010 @ 10:25 PM
- Importance changed from to Low
Can you submit a patch with what you think should be the behavior. Thanks.
-
Robert Pankowecki November 26th, 2010 @ 06:22 PM
- Tag changed from activerecord, module, observed_methods, observers, rails3, rails3.0.3 to activerecord, module, observed_methods, observers, patch, rails3, rails3.0.3
I made the implementation much simpler.
-
Robert Pankowecki November 26th, 2010 @ 06:27 PM
https://gist.github.com/717052 # Overview of the changes
-
Aaron Patterson December 9th, 2010 @ 06:39 PM
sigh. Why didn't observers work like this in the first place?
This patch is fine and I'll apply.
-
Repository December 9th, 2010 @ 06:57 PM
- State changed from new to resolved
(from [61f2d52d8a95ca89ebcb107848890c07b014750c]) Simplifies observer implementation [#6065 state:resolved] https://github.com/rails/rails/commit/61f2d52d8a95ca89ebcb107848890...
-
Repository December 9th, 2010 @ 06:57 PM
(from [bba3dacc3dc6ac379209f2eda0da5d2dd93d6b04]) Simplifies observer implementation [#6065 state:resolved] https://github.com/rails/rails/commit/bba3dacc3dc6ac379209f2eda0da5...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 6064 Exceptions from after_commit and after_rollback from observers are quietly swallowed Could you also spare some of you precious time to this ti...
- 6065 ActiveRecord::Observer is not aware of method added by including modules (from [61f2d52d8a95ca89ebcb107848890c07b014750c]) Simplif...
- 6065 ActiveRecord::Observer is not aware of method added by including modules (from [bba3dacc3dc6ac379209f2eda0da5d2dd93d6b04]) Simplif...
- 6402 ActiveRecord::Observer simplification causes callbacks to be fired multiple times Commit 61f2d52d8 from #6065 causes the same callback from...