# How pagination works in rails

**URL:** <https://www.ruby-forum.com/t/how-pagination-works-in-rails/145840>\
**Category:** Rails\
**Created:** [August 14, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/how-pagination-works-in-rails/145840 "2008-08-14T14:04:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 14, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/how-pagination-works-in-rails/145840/1 "2008-08-14T14:04:56Z")

</div>

Hi,  
I am using the Paginator API for pagination. I have to show 16 objects  
per page. For finding objects I am quering on database and finding all  
objects. After that I am pagination that objects by using the Paginator.  
It is working fine.  
But I found one issue that when I am clicking onto the second page  
link, it is going to controller and again quering on database and loads  
all objects again, depending on the page it is slicing that array of  
object. Can anybody told me it is how the paginator work? or I am wrong?  
If I am right, then Is there is any plugin that hits the database  
only once and fetch records and so that when I click on second page it  
does not hit the database again?  
Thanks & Regards,  
Tushar

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 14, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/how-pagination-works-in-rails/145840/2 "2008-08-14T14:11:33Z")

</div>

On 14 Aug 2008, at 13:04, Tushar G. wrote:

> all objects again, depending on the page it is slicing that array of  
> object. Can anybody told me it is how the paginator work? or I am  
> wrong?

Well that’s certainly one way to do it, but I really wouldn’t do it  
that way.  
Far more efficient is to only load the objects required for each  
individual page.

Fred

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 14, 2008, 2:30pm UTC](https://www.ruby-forum.com/t/how-pagination-works-in-rails/145840/3 "2008-08-14T14:30:20Z")

</div>

I’m not sure, if I’m up to date with that.  
But the problem with Rails pagination is/was, that it  
loads ALL records and then uses some internal methods  
to get the few you want to display (as you describe)  
This is SLOW (and in case of many records can even timeout  
the page request)

There are several ways to avoid that. Most easy is to use  
the :limit and :offset options as described in the API docs:

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

This allows to actually retrieve only what you need.

Another option is to use the will\_paginate gem,  
which makes not only the paginated find more comfortable,  
but comes with some nice view helpers to show the  
pagination links like  
\<\< next 1, 2, 3… 8, 9, 10 next \>\>

> **[Home](https://github.com/mislav/will_paginate/wiki/)**
>
> Pagination library for Rails and other Ruby applications - mislav/will\_paginate

[http://mislav.caboo.se/rails/will\_paginate-love/](http://mislav.caboo.se/rails/will_paginate-love/)

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 14, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/how-pagination-works-in-rails/145840/4 "2008-08-14T14:38:33Z")

</div>

On 14 Aug 2008, at 13:29, Thorsten Müller wrote:

> I’m not sure, if I’m up to date with that.  
> But the problem with Rails pagination is/was, that it  
> loads ALL records and then uses some internal methods  
> to get the few you want to display (as you describe)  
> This is SLOW (and in case of many records can even timeout  
> the page request)

It really doesn’t if you use it right.

paginate :customers, :per\_page =\> 10, :order =\> ‘id desc’  
will use :limit and :offset appropriately

Fred
