Dynamically create a class inherited from ActiveRecord?

后端 未结 1 939
清歌不尽
清歌不尽 2021-01-13 23:14

I want to be able to dynamically create classes, for scripting outside my Rails app, that inherit from ActiveRecord.

I\'m stuck on something like this:



        
相关标签:
1条回答
  • 2021-01-14 00:02

    Nailed it:

    def create_arec(table_name, &block)
      klass = Class.new(ActiveRecord::Base){self.table_name = table_name}
      klass.class_eval &block
      klass
    end
    

    thanks @phoet

    0 讨论(0)
提交回复
热议问题