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
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?
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
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:
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 >>
http://github.com/mislav/will_paginate/wikis http://mislav.caboo.se/rails/will_paginate-love/
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