Skip to content

Commit 3a31499

Browse files
coffeejunkstas
authored andcommitted
Use #size instead of #count for pagination
`#count` will always execute an SQL COUNT query whereas `#size` will check if the records are already loaded (and then call `#length`). It only executes a count when necessary. See https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-size
1 parent 9dee9d8 commit 3a31499

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/jsonapi/pagination.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def jsonapi_pagination_meta(resources)
6363
numbers = { current: page }
6464

6565
if resources.respond_to?(:unscope)
66-
total = resources.unscope(:limit, :offset, :order).count()
66+
total = resources.unscope(:limit, :offset, :order).size
6767
else
6868
# Try to fetch the cached size first
6969
total = resources.instance_variable_get(:@original_size)

0 commit comments

Comments
 (0)