Ruby String to Class Name

后端 未结 7 927
感情败类
感情败类 2020-12-24 12:32

I am trying to create a new class to that will inherit from ActiveRecord::Base the class needs to be dynamically generated from a string

\"gener         


        
7条回答
  •  暖寄归人
    2020-12-24 13:28

    If you're using Rails, it provides a method called #constantize that will work:

    irb(main):017:0> Object.const_get 'House::Owns'
    NameError: wrong constant name House::Owns
    
    'House::Owns'.constantize
    => House::Owns
    

提交回复
热议问题