将不同的查询与Active Record / Arel结合

问题描述

我有一个有效的解决方案,可以组合活动记录过滤器,但我想知道是否可以仅使用一个查询来提高性能。我尝试使用arel_tables,但尝试使用该方法组合多个查询却没有成功。

此刻我是这样的:

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)

在此先感谢您的帮助

解决方法

由于添加了限制,因此无法合并where语句。我从您的查询中得到的是,您正在尝试加载10个接受的用户,25个被拒绝的用户以及所有其他用户。您可以加载所有用户,然后过滤它们。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...