Ruby on rails Rails自动加载和gem类

Ruby on rails Rails自动加载和gem类,ruby-on-rails,gem,autoload,Ruby On Rails,Gem,Autoload,所以我有一个包含ActiveRecord模型的gem # one of 30 or so models used by a suite of applications class MyModel < ActiveRecord::Base # some library code end # in Rails app, I want to add some behavior to this model using good old ruby class reopening class M

所以我有一个包含ActiveRecord模型的gem

# one of 30 or so models used by a suite of applications
class MyModel < ActiveRecord::Base
  # some library code
end

# in Rails app, I want to add some behavior to this model using good old ruby class reopening
class MyModel < ActiveRecord::Base
  scope :active, { where active: true }
end
#一套应用程序使用的大约30种型号之一
类MyModel
问题是,由于Rails,我的应用程序本机版本只能自动加载。因为它是由库加载的,并且常量已注册,所以它永远不会转到本地模型来添加本地定义的行为


除了有一个带有本地版本模型要求的硬编码列表的初始值设定项外,我如何让rails将类的两个定义结合起来,最终得到一个具有所有行为的类?

最简单的解决方案是:

在gem中,将模型名称更改为更通用的名称(MyModelTemplate),并将其抽象化:

class MyModelTemplate < ActiveRecord::Base
  self.abstract_class = true
  # some library code
end
class MyModelTemplate
使您的真实模型继承MyModelTemplate:

class MyModel < MyModelTemplate
end 
类MyModel