ActiveSupport :: DescendantsTracker.descendants not returning children

This question is similar / related to another question about Finding all descendants of a class in Ruby . A wonderful question full of information I was looking for - except when I go down to the rails console:

irb(main):001:0> ActiveSupport::DescendantsTracker.descendants(Object) =>[] irb(main):002:0> ObjectSpace.each_object(Class).select { |klass| klass < Object } => [IRB::Notifier::AbstractNotifier, IRB::Notifier::ErrUnrecognizedLevel, ...] 

So why doesn't ActiveSupport :: DescendantsTracker return the children of the object? What are the differences in implementation? The documentation for DescendantsTracker assumes that:

This module provides an internal implementation for tracking children, which is faster than repetition through ObjectSpace.

Faster? Well, you need to return something faster (what?), But it should return the descendants of the supplied class.

+6
source share
2 answers

ActiveSupport::DescendantsTracker.descendants(Object) will return empty in the console, because the development console will not compile your application, it has not yet loaded all the classes and, therefore, does not know about them to display them!

Take a look at this question: RoR: MyModel.descendants returns [] in the view after the first call?

+2
source

You need to look forward to loading classes as indicated at: https://github.com/rails/rails/issues/3364

 ActionDispatch::Reloader.to_prepare do Rails.application.eager_load! end 
+2
source

Source: https://habr.com/ru/post/945789/


All Articles