I have a simple parent has_many children relationship and I'm trying to get all parents that have less than n children.
Parent.select("parents.id").joins(:children).group('parents.id').having('COUNT(children.id) < ?', n).reorder('parents.id')
The error that keeps appearing is:
SELECT parents.id FROM "parents" INNER JOIN "children" ON "children"."parent_id" = "parents"."id" GROUP BY parents.id HAVING count("children"."id") < 100
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "children"
From what I have read online, this should be working. I've searched through many posts with related questions, but none of the answers seem to relate. There is a scope on the parent-child relationship for ordering, and so that's why I'm reordering by parent id.
What is the "FROM-clause" entry that I need?
Running Rails 4.2 and Postgres
childrentable definitely have bothparent_idandidcolumns? - khampsonchildrendoes haveparent_idandidcolumns. - stevenspiel