nul*_*ull 13 ruby activerecord ruby-on-rails
我正在尝试创建一个Rails插件.在大多数情况下,我所写的作品.然而,关联存在问题.当我尝试调用关联时,我收到此错误:
ActiveRecord::Base doesn't belong in a hierarchy descending from ActiveRecord
Run Code Online (Sandbox Code Playgroud)
目前,插件看起来像这样:
module ControlledVersioning
module ActsAsVersionable
extend ActiveSupport::Concern
included do
has_many :versions, as: :versionable
after_create :create_initial_version
end
module ClassMethods
def acts_as_versionable(options = {})
cattr_accessor :versionable_attributes
self.versionable_attributes = options[:versionable_attributes]
end
end
private
def create_initial_version
version = versions.create
end
end
end
ActiveRecord::Base.send :include, ControlledVersioning::ActsAsVersionable
Run Code Online (Sandbox Code Playgroud)
同样,每当我尝试调用关联时,都会触发错误消息.我在after_create
回调中使用了调试器并尝试运行:
> versions.create
*** ActiveRecord::Base doesn't belong in a hierarchy descending from ActiveRecord
> versions
*** ActiveRecord::Base doesn't belong in a hierarchy descending from ActiveRecord
> Version.new
#<Version id: nil, versionable_id: nil, versionable_type: nil>
Run Code Online (Sandbox Code Playgroud)
我会尝试在初始化程序中扩展活动记录而不是包含它。
初始化器/acts_as_versionable.rb
ActiveRecord::Base.extend(ControlledVersioning::ActsAsVersionable)
也在开发中;或任何重新加载文件的环境,您可能会看到类似的错误has been removed from the module tree but is still active
。确保您的插件文件位于 config.eager_load_paths 中,而不是实际位于关注路径中。