我正在尝试从 gem 扩展 rails 模型。
使用关注我已经能够扩展类方法,但我不能扩展关联。included do
返回undefined method belongs_to
。我认为 Rails 无法正确加载类......
该模型在引擎中,我正在尝试从我的gem访问它。
这是代码:
# 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)
错误。