paginator: fix page counts for relations with group by clauses.

Fix an invalid SQL exception that occurs when the paginator tries to
do a COUNT(*) to calculate the page count of a relation that already
includes a GROUP BY + COUNT(*) clause. We need to nest the whole query
inside a `SELECT COUNT(*) FROM (...)` subquery so the inner COUNT(*)
doesn't mess up the outer COUNT(*).

Fixes #4285.
This commit is contained in:
evazion
2020-02-05 14:21:53 -06:00
parent 6b3c541a8a
commit 089c304992
2 changed files with 3 additions and 3 deletions

View File

@@ -104,7 +104,7 @@ module PaginationExtension
# taken from kaminari (https://github.com/amatsuda/kaminari)
def total_count
@paginator_count ||= except(:offset, :limit, :order).reorder(nil).count
@paginator_count ||= unscoped.from(except(:offset, :limit, :order).reorder(nil)).count
rescue ActiveRecord::StatementInvalid => e
if e.to_s =~ /statement timeout/
1_000_000