ActiveRecord::Base.instantiate_observers does not get called
vihai opened this issue · 5 comments
Hello,
I'm using Rails 5.0.0.1 with latest master rails-observer.
For some reason I couldn't identify, observers listed in config.active_record.observers suddenly stopped being loaded. I traced the issue in reloader.to_prepare's block not being called and thus ActiveRecord::Base.instantiate_observers not being invoked.
Do you have any idea what could be happening?
I don't have any idea. to_prepare is run after the application is loaded and before each request. Please do investigate.
Hi, on another box I installed rails 5.0.0.1 from scratch, created an application with minimal features, I added rails-observers from git to the Gemfile and I added a line to application.rb:
config.active_record.observers = :foobar
I launched rails console and I expected an error to raised however it didn't. In facts the observers are not being loaded. Not in development, neither in production due to reloader.to_prepare's block not being called.
Are you able to reproduce this behaviour?
As a workaround I explicitly added a to_prepare hook that calls ActiveRecord::Base.instantiate_observers. This seemed to work, however Observable.inherited started taking increasing CPU time at each invocation.
My application has a hundred classes, and the time taken for each invocation grew up to 5 seconds each, making the application loading in production extremely slow (2 minutes).
It looks like this may be related to #45
On Rails 5 stable, the syntax listed in the README (i.e., config.active_record.observers) seemed to work but I couldn't figure out how to get that syntax to find my observer with the :class_name syntax once I namespaced it in a module (it never gets called if I just use the class name without the module). I don't see a really compelling reason not to just use ActiveRecord::Base.add_observer UserObserver.instance in an initializer, plus with the class syntax (as opposed to the symbol syntax) it's obvious how to refer to a class which is namespaced in a module, so that's what I'm using now.