© 2017-2022 Domizio DemichelisMIT License

➡ Chat Support on Gitter ➡

Countless Extra

This extra uses the Pagy::Countless subclass in order to save one count query per request. It is especially useful when used with large DB tables, where Caching the count may not be an option, or when there is no need to have a classic UI. Please read also the Pagy::Countless doc for a fuller understanding of its features and limitations.


See extras for general usage info.

In the pagy.rb initializer:

require 'pagy/extras/countless'
# optionally enable the minimal mode by default
# Pagy::DEFAULT[:countless_minimal] = true

In a controller:

# default mode (eager loading)
@pagy, @records = pagy_countless(some_scope, ...)

# OR
# enable minimal mode for this instance (lazy loading)
@pagy, @records = pagy_countless(some_scope, countless_minimal: true, ...)


This extra can be used in two different modes by enabling or not the :countless_minimal variable.

Default mode

This is the preferred automatic way to save one query per request, while keep using the classic pagination UI helpers.

By default this extra will try to finalize the pagy object with all the available variables in a countless pagination. It will do so by retrieving items + 1, and using the resulting number to calculate the variables, while eventually removing the extra item from the result.

That means:

Minimal mode

This is the preferred mode used to implement navless and automatic incremental/infinite-scroll pagination, where there is no need to use any UI.

If you enable the :countless_minimal variable, then:


Variable Description Default
:countless_minimal enable the countless minimal mode false



All the methods in this module are prefixed with the "pagy_countless" string, to avoid any possible conflict with your own methods when you include the module in your controller. They are also all private, so they will not be available as actions. The methods prefixed with the "pagy_countless_get_" string are sub-methods/getter methods that are intended to be overridden, not used directly.

pagy_countless(collection, vars=nil)

This method is the same as the generic pagy method (see the pagy doc), however its returned objects will depend on the value of the :countless_minimal variable (see Modes)

pagy_countless_get_vars(_collection, vars)

This sub-method is similar to the pagy_get_vars sub-method, but it is called only by the pagy_countless method. (see the pagy_get_vars doc).

pagy_countless_get_items(collection, pagy)

This sub-method is similar to the pagy_get_items sub-method, but it is called only by the pagy_countless method. (see the pagy_get_items doc).