[英]Undefined method belongs_to usign Rails concern, why?
我正在嘗試從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)
錯誤的undefined method 'belongs_to' for Myengine::Mymodel:Class (NoMethodError)
。
Myengine::Mymodel
類應該繼承ActiveRecord::Base
有belongs_to
定義方法。
ActiveRecord::Base
包括一堆模塊,其中一個模塊是Associations
,在其中定義了belongs_to association
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.