From 5eb8be55799854c590da1a827cc480aff9d50f6f Mon Sep 17 00:00:00 2001 From: Aliaksandr Buhayeu Date: Wed, 27 Oct 2021 14:42:14 -0700 Subject: [PATCH] Reduce the amount of redundant COUNT queries --- lib/jsonapi/utils/response/formatters.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/jsonapi/utils/response/formatters.rb b/lib/jsonapi/utils/response/formatters.rb index 00f4f2f..7cae6cf 100644 --- a/lib/jsonapi/utils/response/formatters.rb +++ b/lib/jsonapi/utils/response/formatters.rb @@ -134,6 +134,12 @@ def build_response_document(object, options) # @api private def build_collection_result(object, options) records = build_collection(object, options) + + unless options.key?(:count) + limit = page_params['size'].to_i.nonzero? || JSONAPI.configuration.default_page_size + options[:count] = records.size if records.size < limit + end + result_options = result_options(object, options) if related_resource_operation?(options)