Q:

How to declare where clause to retrieve object witth empty has_many association?

Post has_many :images
Image belongs_to :post

I want to select all posts that have no images.
Post.includes(:images).where.not(images: nil) # FAIL

Thanks for any help.

A:

I want to select all posts that have no images

You were close with your where.not thing.
Here is conventional way to achieve it:
Post.includes(:images).where(images: { id: nil })

