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

后端 未结 7 784
礼貌的吻别
礼貌的吻别 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:50

    Note, their is a more efficient way to implement Dave G's method above..

    Object.select(:type).map(&:type).uniq
    

    This first sends marshaled objects that only have the "type" attribute from the DB, which takes WAY less memory, then plucks only the types into an array that you can then uniq on. I'm sure there is an infinitely more efficient pure SQL way to do this though.

提交回复
热议问题