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

Example config only grabs ActiveRecord::Base and ApplicationController #3

Open
MC-Squared opened this issue Jun 20, 2019 · 2 comments

Comments

@MC-Squared
Copy link

Problem

When following the setup laid out in the Readme and using the example initializer I only get entries for classes specified in config.classes_to_monitor, not their subclasses (i.e. ActiveRecord::Base but no models)

This appears to be due to the fact that at the time the initializer is run, Rails has not yet loaded all the classes.
I have tried this on 4.2, 5.2, and 6.0.0.rc1 all with the same results.

It is easy to verify by opening a rails console and running:
ObjectSpace.each_object(ActiveRecord::Base.singleton_class).count

On all 3 apps I get 1, because the models are not yet loaded.

Fix

I am able to get things working as expected by adding:
Rails.application.eager_load! to the top of the initializer.

This is ok, but I wanted to sanity-check if this is expected or if I should be doing something different with the initializer/config.

Happy to put in a PR against the Readme if this fix is the "right way™️" of doing things.

As an aside - Thanks for making this public, it's a very nifty use of Ruby's flexibility. Saw it mentioned in a Railsconf talk and eager to try it out in production.

@MC-Squared
Copy link
Author

MC-Squared commented Oct 8, 2019

Update for anyone who stumbles on this issue: Eager loading is not the "right way ™️".
Loading models from initialisers is not recommended and can cause errors with things like ActiveStorage, see this reply to an ActiveStorage issue.

Given this, I'm not sure what is the correct way to solve the problem here.

@doliveirakn
Copy link
Contributor

Apologies, @MC-Squared I didn't notice that you crated this issue.

It would make sense that in a non-eager loaded environment, it wouldn't have loaded the models yet. We've been using it in our production environment which is eager loaded with config.eager_load = true in config/environments/production.rb

Is the environment you are looking to use this tool in not eager loaded by default?

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

2 participants