如何渴望负荷协会的模式记录的数组 [英] How to eager load association for an array of model records

查看:97
本文介绍了如何渴望负荷协会的模式记录的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Rails的模型记录的数组。是否有可能急于负荷一气呵成(查询),所有的这些记录的关联?

I have an array of Rails model records. Is it possible to eager load an association for all these records in one go (query)?

有时候,我只是只有一个数组,而不是一个AR ::范围。有时我想动态选择什么贪婪加载后。

Sometimes I just only have an array instead of an AR::Scope. And sometimes I want to dynamically choose what to eager load later.

推荐答案

在Rails 3中,可以使用的 preloader 对现有记录急于负载关联。

In Rails 3, one can use preloader to eager-load associations on existing records.

ActiveRecord::Associations::Preloader.new(posts,:comments).run()

这篇关于如何渴望负荷协会的模式记录的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆