Rails 孙子对象带有 :has_many, :through

发布于 2024-12-04 02:09:35 字数 508 浏览 2 评论 0原文

我的模式结构如下:

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 对象。

I have a schema structured as follows:

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

I'm trying to do a select on the Foo model - like Foo.find(:all). Both FooBar and FooBarBaz have the correct foreign key column in the database (foo_id and foo_bar_id, respectively). So how do I access the child and grandchild objects when I access the grandparent object (Foo)?

In the end I need to be iterate through the Foo objects, then through the Foobar objects, then through the FoobarBaz objects, in three nested loops.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风筝在阴天搁浅。 2024-12-11 02:09:35

首先,我将修改语法以匹配约定:

class Foo < ActiveRecord::Base
  has_many :foo_bars
  has_many :foo_bar_bazs, :through => :foo_bars
end


class FooBar < ActiveRecord::Base
  has_many :foo_bar_bazs
  belongs_to :foo_bar
end

Class FooBarBaz < ActiveRecord::Base
  belongs_to :pc_scene_item
end

现在我们有了对象:

Foo.find(:all, :include => [:foo_bars, :foo_bar_bazs])

现在,可以避免 :through,并且您可以执行以下操作:

foos = Foo.find(:all, :include => [{:foo_bars => [:foo_bar_bazs]}])

获取所有子项:

children = foos.collect{|f| f.foo_bars}.flatten.uniq

获取所有孙子项:

grandchildren = foos.collect{|f| f.foo_bars.collect{|b| b.foo_bar_bazs}}.flatten.uniq

First, I'd revise the syntax to match convention:

class Foo < ActiveRecord::Base
  has_many :foo_bars
  has_many :foo_bar_bazs, :through => :foo_bars
end


class FooBar < ActiveRecord::Base
  has_many :foo_bar_bazs
  belongs_to :foo_bar
end

Class FooBarBaz < ActiveRecord::Base
  belongs_to :pc_scene_item
end

Now that we have the objects:

Foo.find(:all, :include => [:foo_bars, :foo_bar_bazs])

Now, :through can be avoided, and you can do:

foos = Foo.find(:all, :include => [{:foo_bars => [:foo_bar_bazs]}])

To get all children:

children = foos.collect{|f| f.foo_bars}.flatten.uniq

To get all grandchildren:

grandchildren = foos.collect{|f| f.foo_bars.collect{|b| b.foo_bar_bazs}}.flatten.uniq
沉溺在你眼里的海 2024-12-11 02:09:35

你不应该使用驼峰命名法来命名你的关联,而应该使用蛇形命名法:

class Foo < ActiveRecord::Base
  has_many :foo_bars
  has_many :foo_bar_bazs, :through => :foo_bars
end

class FooBar < ActiveRecord::Base
  has_many :foo_bar_bazs
  belongs_to :foo
end

class FooBarBaz < ActiveRecord::Base
  belongs_to :foo_bar
end

You shouldn't use camel case to name your associations, but snake case:

class Foo < ActiveRecord::Base
  has_many :foo_bars
  has_many :foo_bar_bazs, :through => :foo_bars
end

class FooBar < ActiveRecord::Base
  has_many :foo_bar_bazs
  belongs_to :foo
end

class FooBarBaz < ActiveRecord::Base
  belongs_to :foo_bar
end
浅笑轻吟梦一曲 2024-12-11 02:09:35

我认为您的 FooBar 模型有错误。我认为你想说它属于 Foo,而不是 FooBar (目前你有 FooBar 属于它自己)。

假设您已准备好所有其他数据和关系,您应该可以将其称为 foo.foo_bar

I think there is a mistake on your FooBar model. I think you intended to say that it belongs_to Foo, not FooBar (currently you have FooBar belong to itself).

Assuming you have all the other data and relationship in place, you should be able to call it foo.foo_bar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文