【发布时间】:2015-07-06 15:26:21
【问题描述】:
下面的问题有一个很好的答案,可以使用Comment.includes(:user) 在一次点击中获取活动记录集合的关联值。当您有多个关联想一次性抓取时怎么办?
Rails have activerecord grab all needed associations in one go?
像下面Customer.includes(:user).includes(:sales).includes(:prices) 那样将它们链接在一起是最好的方法,还是有更清洁的方法。
此外,当我在索引表的循环中执行此操作时。我可以在customer.rb 模型上添加一个方法,以便我可以调用@customers.table_includes 等并拥有
def table_includes
self.includes(:user).includes(:sales).includes(:prices)
end
作为记录,我测试了上述内容,但它不起作用,因为它是一个集合上的方法(尚未弄清楚如何做到这一点)。
【问题讨论】:
标签: ruby-on-rails activerecord