You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
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?
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
, and6.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.
The text was updated successfully, but these errors were encountered: