Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveSupport::DescendantsTracker.descendants not returning descendants

This question is similar / related to another stack overflow question about Looking up all the descendants of a class in Ruby. A wonderful question full of the information that I was looking for - except that when I drop down into a 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 isn't ActiveSupport::DescendantsTracker returning the descendants of Object? What are the differences in the implementation? The documentation for DescendantsTracker suggests that:

This module provides an internal implementation to track descendants which is faster than iterating through ObjectSpace.

Faster? Ok, it's gotta be faster to return nothing versus something (right?), but it's supposed to return the descendants of the supplied class.

like image 272
erroric Avatar asked May 24 '13 14:05

erroric


2 Answers

ActiveSupport::DescendantsTracker.descendants(Object) will be returning blank in your console because the development console doesn't compile your application, it hasn't yet loaded all of the classes and therefore doesn't know about them to output them!

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

like image 92
Matt Avatar answered Nov 16 '22 09:11

Matt


You need to eager load the classes, as stated in: https://github.com/rails/rails/issues/3364

ActionDispatch::Reloader.to_prepare do
  Rails.application.eager_load!
end
like image 3
sequielo Avatar answered Nov 16 '22 09:11

sequielo