Postgresql – Find rows with fewer than X associations (including 0)
I have students associated to schools, and want to find all schools that have five or fewer (including zero) students that have has_mohawk = false. Here's an Activerecord query: School.joins(:students) .group(:id) .having("count(students.id) < 5") .where(students: {has_mohawk: true}) This works for…