How to convert ActiveRecord table name to model class name

前端 未结 3 446
小蘑菇
小蘑菇 2021-01-01 19:55

Is there any possibility to properly convert ActiveRecord table name to model class name? I have found one hack

def model_for_table(table_name)
  table_name.         


        
3条回答
  •  -上瘾入骨i
    2021-01-01 20:05

    ObjectSpace.each_object(Class).select{ |klass| 
      klass < ActiveRecord::Base 
    }.index_by(&:table_name)
    

    It is not the fastest thing in the world though

提交回复
热议问题