Get a list/array of child classes from Single Table Inheritance in Rails?

后端 未结 7 767
礼貌的吻别
礼貌的吻别 2021-02-07 11:49

I have a whole bunch of child classes that inherit from a parent class via single-table-inheritance in my Rails app. I\'d like a way to get an array of all the child classes tha

7条回答
  •  臣服心动
    2021-02-07 12:53

    Rails extends Ruby Class with the subclasses() method.

    In Rails 3 you can call it directly:

    YourClass.subclasses
    

    In Rails 2.3, ".subclasses" is protected, so we use have to call it using send():

    YourClass.send(:subclasses)
    

提交回复
热议问题