Is there a replacement for Rails 3 for subclasses?

I am trying to get all derived classes for a specific type. In Rails 2+, you can say:

Class.subclasses_of SomeClass

This is because activesupport / lib / active_support / core_ext / object / extend.rb went into Rails 3.

Is there a replacement in Rails 3? Are there any alternatives.

+3
source share
3 answers

If you want to get all subclasses of SomeClass, follow these steps:

SomeClass.descendants

Similarly, if you want to learn the parent classes, use:

SomeClass.ancestors

This will give you an array for your consumption. So you can:

SomeClass.descendants.each { |klass| }
+2
source

There is always the possibility of searching in ObjectSpace - something like:

classes = []
ObjectSpace.each_object(Class) do |klass|
  SomeClass > klass
end

, . , , ?

0

Perhaps I misunderstood your question. I never used the subclasses_of method in Rails 2. In Rails 3, I just use Foo.descendantsone that provides an array of classes (and their attributes) that inherit from Foo. Very comfortably.

0
source

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


All Articles