将两个ActiveRecord的查询结果 [英] Combine two ActiveRecord Query results

查看:90
本文介绍了将两个ActiveRecord的查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有两个活动的记录查询,我想结合在一起

I currently have two active record queries that I would like to combine together


joins("join relationships ON user_id = followed_id").
              where("follower_id = #{user.id}")


where(:user_id => user.id)

基本上我想要的第二个结果出现的第一个类似 UNION 语句在SQL。它可以在ActiveRecord的这样做呢?

Basically I want the results of the second one to appear with the first similar to a UNION statement in SQL. Can it be done in ActiveRecord in this way?

我会preFER使用工会相当有参加所有的 followed_id S在一个字符串,并使用在SQL条款。

I would prefer to use a union rather that have to join all the followed_ids in a string and use the IN clause in sql.

任何想法?

-----编辑------ 我要寻找一种方式来获得这个懒惰的加载工作。

-----Edit------ I am looking for a way to get this to work with lazy loading

推荐答案

使用关系和放大器;关于

Model.joins("join relationships ON user_id = followed_id").where("follower_id = {user.id}") & Model.where(:user_id => user.id)

这篇关于将两个ActiveRecord的查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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