Skip to content

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

@MC-Squared

Description

@MC-Squared

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions