Rails的添加自定义的渴望负荷 [英] Rails add custom eager load

查看:97
本文介绍了Rails的添加自定义的渴望负荷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Rails中自定义的find_by_sql查询。我想用预先加载与他们,但似乎没有成为一个很好的办法做到这一点。

I have a number of custom find_by_sql queries in Rails. I would like to use eager loading with them but there doesn't seem to be a good way to do this.

我已经看到了eager_custom.rb文件漂浮,它似乎并没有现在使用Rails。它似乎Rails会预先加载不同,现在,使用2个查询(定期查询以及查询那里的'身份证IN从第一次查询的ID),而不是过去使用单一的连接查询。

I have seen the eager_custom.rb file floating around and it doesn't seem to work with Rails now. It appear Rails does eager loading differently now, using 2 queries (the regular query plus a query where the 'id IN' the ids from the first query), instead of the single join query used in the past.

我的问题是,如果我做一个自定义的SQL查询,然后做'身份证IN的查询,有没有办法重新添加相关的对象到初始查询结果?

My question is if I do a custom SQL query, then do 'id IN' query, is there a way to add back associated objects into the initial query results?

例如我有话题满载的find_by_sql,那么我觉得题目图像该主题id的主题标识,有没有办法来手动添加回图像的主题?

For example I have topics loaded with find_by_sql, then I find topic images where the topic id is in the topics ids, is there a way to add the images manually back to the topics?

感谢

推荐答案

正如你注意到,在Rails的2.1一种新的渴望/ $的对$ P装引入,它使用多个查询与 ID IN(...)。这种方法通常更快,尤其是当有多个关联是$ P $对 - 加载。您可以通过使用ActiveRecord的(不推荐)继承了 preload_associations 类方法手动使用此功能与的find_by_sql 。例如:

As you noticed, in Rails 2.1 a new kind of eager/pre-loading was introduced which uses multiple queries with id IN (...). This method is usually faster, especially when there are multiple associations being pre-loaded. You can use this functionality manually with find_by_sql by using the preload_associations class method inherited from ActiveRecord (not recommended). For example:

class Person
  def self.find_a_special_group
    people = find_by_sql("...")
    preload_associations(people, [:jobs, :addresses])
    return people
  end
end

preload_associations 方法是受保护的,所以你必须从类中调用它,它需要的对象(1)为一组,(2)数组,哈希,或协会的符号(相同的格式查找:包括选项),和(3)期权哈希值。见ActiveRecord的::协会preLOAD :: ClassMethods模块的文档获取更多细节。

The preload_associations method is protected, so you must call it from within the class, and it takes (1) an array of objects, (2) an array, hash, or symbol of associations (same format as find's :include option), and (3) an options hash. See the documentation for the ActiveRecord::AssociationPreload::ClassMethods module for more details.

不过,话说回来了这一切,这个技术是为Rails文档的情况很不理想使用直接阻碍了程序员 preload_associations 。你确定你必须使用的find_by_sql ?你确定你知道所有的选项查找需要? (:选择:从:加入:组:有等),我不是说你不需要的find_by_sql ,但它可能是值得几分钟,以确保。

However, having said all of that, this technique is certainly undesirable as the Rails documentation discourages programmers from using preload_associations directly. Are you sure you have to use find_by_sql? Are you sure you know all of the options find takes? (:select, :from, :joins, :group, :having, etc) I'm not saying you don't need find_by_sql, but it might be worth a few minutes to make sure.

这篇关于Rails的添加自定义的渴望负荷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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