简体   繁体   English

使用 Active Record/Arel 组合不同的查询

[英]Combine different queries with Active Record/Arel

I have a working solution on combining active record filters but I was wondering if it's possible to use just one query to make this more performant.我有一个结合活动记录过滤器的工作解决方案,但我想知道是否可以只使用一个查询来提高性能。 I tried using arel_tables but I was unsuccessful trying to combine multiple queries using that method.我尝试使用arel_tables但尝试使用该方法组合多个查询没有成功。

What I have at the moment is this:我目前所拥有的是:

not_accepted_ids = User.where.not(status: ['accepted', 'rejected']).pluck(:id)

accepted_ids = User.where(status: 'accepted').order('updated_at DESC').limit(25).pluck(:id)

rejected_ids = User.where(status: 'rejected').order('updated_at DESC').limit(10).pluck(:id)

@users = User.where(id: not_accepted_ids + accepted_ids + rejected_ids)

Thanks in advance for any help在此先感谢您的帮助

Since you have limits added, you cannot combine where statements.由于添加了限制,因此无法组合 where 语句。 What I am getting from your query is you are trying to load 10 accepted users, 25 rejected users and all of the other users.我从您的查询中得到的是,您正在尝试加载 10 个接受的用户、25 个拒绝的用户和所有其他用户。 You can load all of the users and then filter them instead.您可以加载所有用户,然后对其进行过滤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM