Ok, I tried to answer above, but had a problem.
for our purposes Father has_many: sons, okay?
I wanted to find fathers who had zero sons.
the above did not work because it created an inner connection ... thus filtering out all fathers without sons.
the following worked for me:
Father.includes(:sons).group('fathers.id').having( 'count(sons.id)=0' )
and this also works for any other filter you need
Father.includes(:sons).group('fathers.id').having( 'count(sons.id)=3' )
source
share