gpt4 book ai didi

ruby-on-rails - 是否可以做一个双 has_many :through association in rails?

转载 作者:数据小太阳 更新时间:2023-10-29 07:21:04 25 4
gpt4 key购买 nike

我有四个模型:a、b、c、d

这是我想做的:

a has_many b, :through => c
a has_many d, :through => b

这样我就可以在控制台中执行以下操作:

a.b
a.b.first.d
a.d

目前前两个命令有效,但在第三个命令中出现 SQL 错误。它似乎试图直接通过 b 得到 d 而没有发现 a -> b 通过 c 的事实。

如何解决?

最佳答案

为遇到它的任何人快速更新,这在 Rails 3.1 之后是可能的:http://guides.rubyonrails.org/3_1_release_notes.html

在您的示例中,它是这样的:

Class A
has_many :c
has_many :b, :through => :c, :source => :b
has_many :d, :through => :b, :source => :d
end

Class C
has_many :b
has_many :d, :through => :b, :source => :d
end

Class B
has_many :d
end

Class D

end

只是为了澄清!

关于ruby-on-rails - 是否可以做一个双 has_many :through association in rails?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5233402/

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