簡體   English   中英

未定義的方法歸屬於usign Rails,為什么?

[英]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::Basebelongs_to定義方法。

ActiveRecord::Base包括一堆模塊,其中一個模塊是Associations ,在其中定義了belongs_to association

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM