If you use .merge, Rails generates a subquery for you (with a ton of extra joins). It will basically end up as an additional where clause that says WHERE "students"."id" IN (subquery). I'm just saying you can do that subquery manually and avoid the extra joins. You can still sort in the external query.