gpt4 book ai didi

ruby-on-rails - ActiveRecord::Base 不属于从 ActiveRecord 降级的层次结构

转载 作者:数据小太阳 更新时间:2023-10-29 06:39:44 24 4
gpt4 key购买 nike

我正在尝试创建一个 Rails 插件。在大多数情况下,我写的作品。但是,关联存在问题。当我尝试调用关联时,出现此错误:

ActiveRecord::Base doesn't belong in a hierarchy descending from ActiveRecord

目前,插件看起来像这样:

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

同样,每当我尝试调用关联时都会触发错误消息。我在 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>

最佳答案

为了使其正常工作,您需要更改代码中的一些内容。

首先,versions 是 rails 的一个保留键盘——你不能与那个名称有关系——(我使用名称 versionings 来使它工作)

此外,您要确保只为想要acts_as_versionable 的模型添加has_many 版本控制 - 意思是,移动has_many :versionings, as: versionable、class_name: 'Version'after_create :create_initial_version 调用 acts_as_versionable 方法。

下面是所有内容的样子:

module ControlledVersioning
module ActsAsVersionable
extend ActiveSupport::Concern

module ClassMethods
def acts_as_versionable(options = {})
has_many :versionings, as: :versionable, class_name: 'Version'
after_create :create_initial_version

cattr_accessor :versionable_attributes
self.versionable_attributes = options[:versionable_attributes]
end
end

private

def create_initial_version
version = versionings.create
end
end
end

ActiveRecord::Base.send :include, ControlledVersioning::ActsAsVersionable

做这些改变使插件对我有用:

irb(main):003:0> Post.create!
(0.1ms) begin transaction
Post Create (0.7ms) INSERT INTO "posts" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2019-07-16 08:55:13.768196"], ["updated_at", "2019-07-16 08:55:13.768196"]]
Version Create (0.2ms) INSERT INTO "versions" ("versionable_type", "versionable_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["versionable_type", "Post"], ["versionable_id", 3], ["created_at", "2019-07-16 08:55:13.772246"], ["updated_at", "2019-07-16 08:55:13.772246"]]
(2.0ms) commit transaction
=> #<Post id: 3, created_at: "2019-07-16 08:55:13", updated_at: "2019-07-16 08:55:13", name: nil>
irb(main):004:0> Post.last.versionings
Post Load (0.2ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT ? [["LIMIT", 1]]
Version Load (0.2ms) SELECT "versions".* FROM "versions" WHERE "versions"."versionable_id" = ? AND "versions"."versionable_type" = ? LIMIT ? [["versionable_id", 3], ["versionable_type", "Post"], ["LIMIT", 11]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Version id: 2, versionable_type: "Post", versionable_id: 3, created_at: "2019-07-16 08:55:13", updated_at: "2019-07-16 08:55:13">]>
irb(main):005:0>

关于ruby-on-rails - ActiveRecord::Base 不属于从 ActiveRecord 降级的层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21360392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com