简体   繁体   中英

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.

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. 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. You can load all of the users and then filter them instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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