From 089c30499203980b0fe8a0e94eacd58753e01311 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 5 Feb 2020 14:21:53 -0600 Subject: [PATCH] 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. --- app/logical/pagination_extension.rb | 2 +- app/views/layouts/default.html.erb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/logical/pagination_extension.rb b/app/logical/pagination_extension.rb index a4d367be4..27963ac5b 100644 --- a/app/logical/pagination_extension.rb +++ b/app/logical/pagination_extension.rb @@ -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 diff --git a/app/views/layouts/default.html.erb b/app/views/layouts/default.html.erb index 9eaafc6f2..f0abf5cee 100644 --- a/app/views/layouts/default.html.erb +++ b/app/views/layouts/default.html.erb @@ -5,11 +5,11 @@ - <% if (@current_item.try(:prev_page) rescue false) %> + <% if @current_item.try(:prev_page) %> <%= tag.link rel: "prev", href: url_for(nav_params_for(@current_item.prev_page)) %> <% end %> - <% if (@current_item.try(:next_page) rescue false) %> + <% if @current_item.try(:next_page) %> <%= tag.link rel: "next", href: url_for(nav_params_for(@current_item.next_page)) %> <% end %>