Undefined method belongs_to usign Rails 关注,为什么?

Undefined method belongs_to usign Rails concern, why?

我正在尝试从 gem 扩展 rails 模型。

使用 concern 我已经能够扩展 class 方法 但我无法扩展 associations included do returns undefined method belongs_to。我认为 Rails 无法正确加载 class...

模型引擎中,我正在尝试从我的[=44=访问它].

代码如下:

# mygem/config/initializers/mymodel_extension.rb
require 'active_support/concern'

module MymodelExtension
  extend ActiveSupport::Concern

  #  included do
  #    belongs_to :another
  #  end

  class_methods do
    def swear
        return "I'm not doing it again"
    end
  end

end

class Myengine::Mymodel
    include MymodelExtension
end

从命令行:

Myengine::Mymodel.swear
# => "I'm not doing it again"

如果我取消对 included do 的注释,我会收到此 undefined method 'belongs_to' for Myengine::Mymodel:Class (NoMethodError) 错误。

Myengine::Mymodelclass 应该继承自 ActiveRecord::Base 以定义 belongs_to 方法。

ActiveRecord::Base 包括一堆模块,其中一个是 Associations, where belongs_to association 已定义。