✳ Paginators
pagy Method
The pagy method provides a common interface to all paginators. Include it where you are going to paginate a collection (usually in ApplicationController):
include Pagy::Method
You can use it to paginate ANY collection, with ANY technique. For example:
@pagy, @records = pagy(:offset, collection, **options)
@pagy, @records = pagy(:keyset, set, **options)
@pagy, @records = pagy(...)
:offset,:keyset, etc. are symbols identifying thepaginator . They implement the specific pagination.@pagyis the pagination instance. It provides all the instance helper methods to use in your code.@recordsare the records belonging to the requested page.
The pagy method expects to find the rack request at self.request, however, you can also use pagy outside controllers or views, or pass your own :request option.
Paginators
Read also the Choose Right Guide to ensure good performance and smooth workflow.
The paginators are symbolic names of different pagination types/contexts (e.g., :offset, :keyset, countless, etc.). You pass the name to the pagy method and pagy will internally instantiate and handle the appropriate paginator class.
Avoid instantiating Pagy classes directly
Instantiate paginator classes only if the documentation explicitly suggests it.
Paginators and classes are autoloaded only if used!
Unused code consumes no memory.
PostgreSQL collections must be ordered.
Chain something like .order(:id) to your collection. See the PostgreSQL Documentation