gpt4 book ai didi

ruby-on-rails - 嵌套 has_many

转载 作者:行者123 更新时间:2023-12-03 15:29:25 24 4
gpt4 key购买 nike

我正在使用 Rails 2.3.5。

Class User < ActiveRecord::Base
has_many :phones
end

class Phone < ActiveRecord::Base
has_many :frequency_bands
end

我想获取用户的所有频率带。我知道我可以为用户编写一个 def freq_bands 方法,但我想知道是否可以为用户使用 has_many freq_bands。这样我就可以链接调用。

我想要的是

class User < ActiveRecor::Base
has_many :frequence_bands, :through => phones
end

我认为使用这个插件可以嵌套 has_many http://github.com/ianwhite/nested_has_many_through

但是,如果可能的话,我想避免使用其他插件并完全依赖 Rails。

最佳答案

class User < ActiveRecord::Base
has_many :phones
has_many :frequence_bands, :through => :phones
end

工作正常。如果电话本身也是 has_many_through 关系,那么您只需要嵌套的 has_many_through 插件,而您的示例中没有这种关系。

(编者:别忘了最后一个属性前面的“:”)

关于ruby-on-rails - 嵌套 has_many,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2508360/

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