For a very long time in Rails,
it was not possible to combine delete statements with GROUP_BY and HAVING queries.
It does seem like something that should have been supported out of the box,
but unfortunately was overlooked — until now!
Before
Using GROUP_BY or HAVING in a delete statement was ignored.
Let’s look at an example.
Here,
a user has multiple votes,
and
we’re trying to count users who have less than three votes.
If we do update_all with group and having clause ActiveRecord would ignore it.
However, when we try to delete these users, we end up with an error.
Interestingly,
MySQL does not support using DELETE or UPDATE statements in combination with GROUP_BY or HAVING.
To circumvent this,
the PR uses nested queries.