gpt4 book ai didi

ruby-on-rails - 为什么我的简单的 self 参照关联会出现 inverse_of 错误?

转载 作者:行者123 更新时间:2023-12-04 14:48:13 25 4
gpt4 key购买 nike

我有这样一个模型:

class Thing < ApplicationRecord
belongs_to :thing
has_many :things
end

在 rails 5 中这是有效的。在 Rails 6.1 中,会发生这种情况:

t1 = Thing.create
t2 = Thing.create(thing: t1)
t3 = Thing.create(thing: t2)

t3.thing.id # t2 id, correct
Thing.find(t3.thing_id).thing.id # t1 id, correct
t3.thing.thing.id # t3 id, incorrect! for some reason it loops back when loading this record

行为是通过添加 inverse_of 修复的:

class Thing < ApplicationRecord
belongs_to :thing, inverse_of: :things
has_many :things, inverse_of: :thing
end

这是错误还是预期的行为?

最佳答案

关于ruby-on-rails - 为什么我的简单的 self 参照关联会出现 inverse_of 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69569233/

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