ARel模拟包含find_by_sql [英] ARel mimic includes with find_by_sql

查看:118
本文介绍了ARel模拟包含find_by_sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当复杂的sql查询,我确定我无法完成使用ARel(Rails 3.0.10)

I've got a fairly complex sql query that I'm pretty sure I can't accomplish with ARel (Rails 3.0.10)

检查链接,但是它具有一些联接和where exists子句,并且我确定对于ARel来说太复杂了.

Check out the link, but it has a few joins and a where exists clause, and that I'm pretty sure is too complex for ARel.

但是,我的问题是,在此查询如此复杂之前,使用ARel我可以使用includes来添加其他需要避免n + 1问题的模型.现在,我正在使用find_by_sql,includes不起作用.我仍然希望能够像includes那样获取这些记录并将它们附加到我的模型实例中,但是我不确定如何实现.

My problem however is that, before this query was so complex, with ARel I could use includes to add other models that I needed to avoid n+1 issues. Now that I'm using find_by_sql, includes don't work. I still want to be able to fetch these records and attach them to my model instances, the way includes does, but I'm not quite sure how to achieve this.

有人可以指出我正确的方向吗?

Can someone point me in the right direction?

我还没有尝试将它们加入同一查询中.我只是不确定如何将它们映射到对象(即ActiveRecord是否可以将它们正确映射到适当的类)

I haven't tried joining them in the same query yet. I'm just not sure how they would be mapped to objects (ie. if ActiveRecord would properly map them to the proper class)

我知道在使用includes时,ActiveRecord实际上会进行第二次查询,然后以某种方式将这些行附加到原始查询的相应实例中.有人可以指导我如何做吗?还是我需要加入相同的查询?

I know that when using includes ActiveRecord actually makes a second query, then somehow attaches those rows to the corresponding instances from the original query. Can someone instruct me on how I might do this? Or do I need to join in the same query?

推荐答案

让我们假装真的无法将SQL简化为Arel.并非一切都可以,而且我们碰巧真的想保留自定义的find_by_sql,但我们也想使用include.

Let's pretend that the SQL really can't be reduced to Arel. Not everything can, and we happen to really really want to keep our custom find_by_sql but we also want to use includes.

然后preload_associations是您的朋友: (已针对Rails 3.1更新)

Then preload_associations is your friend: (Updated for Rails 3.1)

class Person
  def self.custom_query
    friends_and_family = find_by_sql("SELECT * FROM people")
# Rails 3.0 and lower use this: 
#        preload_associations(friends_and_family, [:car, :kids])
# Rails 3.1 and higher use this: 
    ActiveRecord::Associations::Preloader.new(friends_and_family, [:car, :kids]).run
    friends_and_family
  end
end

请注意,3.1方法要好得多,b/c您可以随时应用eager-loading.因此,您可以在控制器中获取对象,然后在呈现之前,可以检查格式并渴望加载更多关联.对我来说就是这样-html不需要急切的加载,但是.json确实需要.

Note that the 3.1 method is much better, b/c you can apply the eager-loading at any time. Thus you can fetch the objects in your controller, and then just before rendering, you can check the format and eager-load more associations. That's what happens for me - html doens't need the eager loading, but the .json does.

有帮助吗?

这篇关于ARel模拟包含find_by_sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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