我试图从gem扩展rails模型。
使用关注我已经能够扩展类方法,但我无法扩展关联。 undefined method belongs_to
返回# 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
。我认为Rails无法正确加载该类......
模型位于引擎中,我试图从我的 gem 访问它。
以下是代码:
Myengine::Mymodel.swear
# => "I'm not doing it again"
从命令行:
included do
如果我取消注释undefined method 'belongs_to' for Myengine::Mymodel:Class (NoMethodError)
,我会收到此git pull
错误。
答案 0 :(得分:2)
Myengine::Mymodel
类应继承ActiveRecord::Base
以定义belongs_to
方法。
ActiveRecord::Base
包含大量模块,其中一个是Associations
,其中定义了belongs_to association
。