为什么我的简单自我参照关联会出现 inverse_of 错误?
Why is my simple self-referential association getting inverse_of wrong?
我有这样一个模型:
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
这是错误还是预期行为?
我有这样一个模型:
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
这是错误还是预期行为?