Besides the exists? It would be great support for something like:
Post.exists(User.active.where("users.id = posts.user_id"))
# SELECT posts.* from posts WHERE EXISTS (SELECT 1 FROM users WHERE users.active = true AND users.id = posts.user_id)
I want a list a of posts for the active users⌠Not a boolean.
It would be an alternative to Posts.where(user: User.active), that depending the case is less performant than EXISTS. (IN vs EXISTS)
Yeah it could do the thing⌠But there are many others situations that an exists clause would be the best option. And rails doesnât support it out of the box.
I see what you mean. But maybe @Lorin_Thwaitsâs comment kind of proves why the language might be confusing. exists? is pretty prevalent and to add a similar method that gives very different results could be confusing. I suspect that it would also be opening up a can of worms (multiple exists, nested exists) that might be more than youâre bargaining for. Iâd prefer to hear the opinion of someone whoâs actually contributed to this part of Rails, though.
Ah! Now I more clearly understand your goal, and realise that #active is likely a scope youâve defined in the User model. If youâre okay to have the same kind of logic from that scope be instead in a .where then I can recommend: