Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord::Base.instantiate_observers does not get called #52

Closed
vihai opened this issue Aug 23, 2016 · 5 comments
Closed

ActiveRecord::Base.instantiate_observers does not get called #52

vihai opened this issue Aug 23, 2016 · 5 comments

Comments

@vihai
Copy link

vihai commented Aug 23, 2016

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?

@rafaelfranca
Copy link
Member

I don't have any idea. to_prepare is run after the application is loaded and before each request. Please do investigate.

@vihai
Copy link
Author

vihai commented Aug 24, 2016

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?

@vihai
Copy link
Author

vihai commented Aug 31, 2016

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).

ulferts added a commit to oliverguenther/openproject that referenced this issue Sep 8, 2016
The observers should have been removed a long time ago and with rails 5 there is
rails/rails-observers#52
to consider as well.
@hut8
Copy link

hut8 commented Nov 27, 2016

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.

@kaspth
Copy link
Contributor

kaspth commented Jul 16, 2017

I've only skimmed this issue, but it sounds related to the fix in #65. Try the new 0.1.5!

@kaspth kaspth closed this as completed Jul 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants