【发布时间】:2023-03-23 17:56:01
【问题描述】:
我的架构结构如下:
class Foo < ActiveRecord::Base
has_many :foo_bars
has_many :foo_bar_bazs, :through => :foo_bars
end
class FooBar < ActiveRecord::Base
has_many :foo_bars
belongs_to :foo_bar
end
Class FooBarBaz < ActiveRecord::Base
belongs_to :foo_bar
end
我正在尝试对 Foo 模型进行选择 - 例如 Foo.find(:all)。 FooBar 和 FooBarBaz 在数据库中都有正确的外键列(分别为 foo_id 和 foo_bar_id)。那么在访问祖父对象(Foo)时如何访问子对象和孙对象呢?
最后我需要在三个嵌套循环中遍历 Foo 对象,然后遍历 Foobar 对象,然后遍历 FoobarBaz 对象。
【问题讨论】:
标签: ruby-on-rails rails-activerecord