我无法找出通过关联链接检索多个父母的所有孩子的正确方法。是:grandparent.parents.children关联链接在Rails 4中不正确?
为了简化我有三个型号:
class Customer < ActiveRecord::Base
has_many :invoices
end
class Invoice < ActiveRecord::Base
belongs_to :customer
has_many :line_items
end
class LineItem < ActiveRecord::Base
belongs_to :invoice
end
创建几个对象我厌倦了使用从导轨导向的例子后(协会基础:4.3.3.4包括):
Customer.first.invoices.line_items
它退货:
undefined method `line_items' for #<Customer::ActiveRecord_Associations_CollectionProxy
是否grandparent.parents.children不可用?
编辑
我没有搜索的grandparent.parents.first.children,但集合中的所有父母的孩子,导轨导游状态:
如果经常检索(@ customer.orders.line_items),
作为一个有效的操作,我想知道这是否是一个错误。
FINAL正如所选答案的评论所述:在ActiveRecord中:范围是可链接的,但关联不是。
我知道这一点,我感兴趣的是链接协会可用。 – quad
您可以链接范围,但不是关联。所以因为工作范围上的关系,并在单个记录的关联(例如一张发票),你可以写'customer.invoices.active'但不是'customer.invoices.line_items'。 – nathanvda
这就是我正在寻找的。 – quad