Wednesday, 11 September 2013

What's the easiest way to select Object(s) that don't have nested objects?

What's the easiest way to select Object(s) that don't have nested objects?

Say I have Item that has_many Posts.
And I need to select Items that don't have ANY Posts.
My current solution is this:
Item.where("NOT EXISTS (SELECT 1 FROM posts p WHERE p.item_id = items.id)")
Is that the best way? Maybe should use OUTER JOIN somehow?

No comments:

Post a Comment