未定义的方法belongs_to使用Rails关注,为什么?

时间:2016-09-23 18:19:54

标签: ruby-on-rails ruby ruby-on-rails-4 rubygems activesupport-concern

我试图从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错误。

1 个答案:

答案 0 :(得分:2)

Myengine::Mymodel类应继承ActiveRecord::Base以定义belongs_to方法。

ActiveRecord::Base包含大量模块,其中一个是Associations,其中定义了belongs_to association