diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dd1d1f..61228c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ These are the latest changes on the project's `master` branch that have not yet ### Changed - **Breaking change:** The way records are retrieved from a given cursor has been changed to no longer use `CONCAT` but instead simply use a compound `WHERE` clause in case of a custom order and having both the custom field as well as the `id` field in the `ORDER BY` query. This is a breaking change since it now changes the internal order of how records with the same value of the `order_by` field are returned. +- Remove `ORDER BY` clause from `COUNT` queries ## [0.1.3] - 2021-03-17 diff --git a/lib/rails_cursor_pagination/paginator.rb b/lib/rails_cursor_pagination/paginator.rb index 9d7f5a7..9a93cbe 100644 --- a/lib/rails_cursor_pagination/paginator.rb +++ b/lib/rails_cursor_pagination/paginator.rb @@ -159,7 +159,7 @@ def page # # @return [Integer] def total - memoize(:total) { @relation.size } + memoize(:total) { @relation.reorder('').size } end # Check if the pagination direction is forward @@ -186,11 +186,12 @@ def previous_page? # When paginating forward, we can only have a previous page if we were # provided with a cursor and there were records discarded after applying # this filter. These records would have to be on previous pages. - @cursor.present? && filtered_and_sorted_relation.size < total + @cursor.present? && + filtered_and_sorted_relation.reorder('').size < total else # When paginating backwards, if we managed to load one more record than # requested, this record will be available on the previous page. - @page_size < limited_relation_plus_one.size + @page_size < limited_relation_plus_one.reorder('').size end end @@ -201,12 +202,12 @@ def next_page? if paginate_forward? # When paginating forward, if we managed to load one more record than # requested, this record will be available on the next page. - @page_size < limited_relation_plus_one.size + @page_size < limited_relation_plus_one.reorder('').size else # When paginating backward, if applying our cursor reduced the number # records returned, we know that the missing records will be on # subsequent pages. - filtered_and_sorted_relation.size < total + filtered_and_sorted_relation.reorder('').size < total end end