在 rails 中收集对象

Collecting object in rails

我是新手,在 rails 中收集对象时遇到问题。 我有 3 个模型: 一个 has_many B (B belongs_to A) Bhas_many C (c belongs_to B) 当我得到 A 时,如何从 A 中收集所有 C,如下所示: A.Bs.Cs 请帮助我!!!

有点简单,Rails完全支持!

class A < ActiveRecord::Base
  has_many :bs # has many B
  # The point here
  has_many :cs, through: :bs # has many C through B
end

class B < ActiveRecord::Base
  has_many :cs
  belongs_to :a
end

class C < ActiveRecord::Base
  belongs_to :c
end

例如,查询将是

A.find(1).cs

顺便说一句,您应该使用更多描述性信息来修改您的问题,A, B, C 有点令人困惑!