There are two Gems which are very famous in the Rails World for Pagination.

  1. Kaminari
  2. Will paginate

These both gems are widely used because of their simple and easy configuration.

Today, we are going to learn about a new gem for pagination.

Pagy is way much faster than Kaminari and Will paginate. Its size and simplicity are one of the reasons for its stunning performance.

Including all the features, validations, exceptions, etc. Pagy is just 3 small modules of just ~100 lines in total, easy to understand and use. The code contains just three main modules

  1. Pagy
  2. Pagy::Backend
  3. Pagy::Frontend

Features

  • It has no dependencies, it produces its own HTML, URLs, i18n with its own specialized and fast code
  • 100% of its methods are public API, accessible and overridable right where you use them (no monkey-patching needed)
  • It has a quite fat set of optional extras that you can explicitly require for very efficient and modular customization
  • The Pagy class doesn’t need to know anything about your models, ORM or storage, so it doesn’t add any code to them
  • It works with all kinds of collections, even pre-paginated, records, Arrays, JSON data, and just any list, even if you cannot count it.
  • Pagy works with the most popular Rack frameworks (Rails, Sinatra, Padrino, etc.) out of the box.
  • Pagy works well with the results of the ransack search and elastic search.

Read Also: Most useful Ruby on Rails Gems

Installation & Use

As we do for other gems write below the line in gem file and hit bundle install command.

gem 'pagy'

Include back-end module in the controller where you want to use pagination

include Pagy::Backend

Use the pagy method on returning results of the query as below

@pagy, @records = pagy(Product.limit(10))

Include front-end helper in some helper module as below

include Pagy::Frontend

Render the navigation links in view template as below

<%= pagy_nav(@pagy) %>

or with a partial:

<%= render 'pagy/nav', locals: {pagy: @pagy} %>

You can customize some defaults configuration globally by creating pagy.rb file in initializer directory

You can copy the comprehensive and annotated pagy.rb and uncomment and edit what you may need. The file also contains all the relevant documentation links.

Pagy supports other front-end libraries which Kaminari and Will paginate supports like

Bootstrap, Materialize, Foundation, Bulma, etc.

Comparison with other Gems

The best way to quickly get an idea about Pagy is by comparing it to the other well-known gems.

The values shown in the charts below have been recorded while each gem was producing the exact same output in the exact same environment.

~ 33x Faster!

Output with Pagy - pagination ruby gem

~ 26x Lighter!

Outout ith Pagy pagination rails gem

There is a migration guide available if you want to switch from Kaminari or Will paginate.

So, here is the new choice of a gem for pagination which is worth to use in your next project.

Click here for more details…


At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.

We also specialize in RPA, AI, Python, Django, JavaScript and ReactJS.

Consulting is free – let us help you grow!