# Pagination problem

**URL:** <https://www.ruby-forum.com/t/pagination-problem/175161>\
**Category:** Rails\
**Created:** [September 9, 2009, 8:06am UTC](https://www.ruby-forum.com/t/pagination-problem/175161 "2009-09-09T08:06:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkat\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4af34b/32.png) [@venkat\_ruby](https://www.ruby-forum.com/u/venkat_ruby)\
**Post date:** [September 9, 2009, 8:06am UTC](https://www.ruby-forum.com/t/pagination-problem/175161/1 "2009-09-09T08:06:34Z")

</div>

Hi all,

In my controller  
def index  
@customers = Customer.find(:all)  
@size = @customers.size  
@total\_pages = (@customers.size / $PER\_PAGE.to\_f).ceil  
end

In index.html.erb  
Displays a customer list with pagination(Example: 1,2,3,4,5…)

On each page number click (Example: 1,2,3,4,5…) - the below code in  
index function gets executed.

@customers = Customer.find(:all)  
@size = @customers.size  
@total\_pages = (@customers.size / $PER\_PAGE.to\_f).ceil

This results in performace lack.

So I want to read the db records only once and store it in some  
variable.  
And i will use that variable to display the records for 2,3,4,5 pages.

Please note: I don’t want to use any plugins and please help to  
implement…

---

<div class="post-metadata">

**Author:** ![venkat\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4af34b/32.png) [@venkat\_ruby](https://www.ruby-forum.com/u/venkat_ruby)\
**Post date:** [September 9, 2009, 8:28am UTC](https://www.ruby-forum.com/t/pagination-problem/175161/2 "2009-09-09T08:28:39Z")

</div>

hey dude.  
I think by your requirement, you’d better use the ajax for the  
pagination.  
read all the records from DB in your index action, and latter use ajax  
for the pagination(you will not need access your index action again )

BTW: will you use cache in your app? if, yes your way is not bad  
either. Though every time when you selecting the page, you will access  
the index action. but it will not get the records from your DB , it  
will get from your cache.

I prefer the cache one.

Hope it gives you some help.

yours,  
Terry

2009/9/9 Venkat E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![venkat\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4af34b/32.png) [@venkat\_ruby](https://www.ruby-forum.com/u/venkat_ruby)\
**Post date:** [September 9, 2009, 8:34am UTC](https://www.ruby-forum.com/t/pagination-problem/175161/3 "2009-09-09T08:34:30Z")

</div>

will\_paginate plugin is best solution

It can retrieve records for particular page, not whole record set.

@customers = Customer.paginate( :page =\> :params[:page], :limit =\> 10 )

Sandip R~

On Wed, Sep 9, 2009 at 11:56 AM, Terry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Hi all,
> > 
> > variable.

–  
Ruby on Rails Developer

> **[Blogs - Neuigkeiten - Artikel - Webseiten - Medien](https://www.sosblog.com/)**
>
> Auf SOSblog geht es um aktuelle Blogs, Neuigkeiten, Artikel, Webseiten und Medien. Wir bringen interessante neue Webseiten aus den Tiefen des Webs.

> **[Judul Situs](https://funonrails.wordpress.com/)**
>
> “Cinta tidak mengenal batas. Cinta mampu melewati rintangan, melompati pagar, menembus dinding untuk mencapai tujuannya dengan penuh harapan.” — Maya Angelou

> **[Josh Software | Custom Software Development Services & Solutions](https://joshsoftware.com/)**
>
> JoshSoftware offers custom software development Services & Solution that provides enterprise, software development services for web & mobile.

---

<div class="post-metadata">

**Author:** ![venkat\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4af34b/32.png) [@venkat\_ruby](https://www.ruby-forum.com/u/venkat_ruby)\
**Post date:** [September 9, 2009, 9:18am UTC](https://www.ruby-forum.com/t/pagination-problem/175161/4 "2009-09-09T09:18:25Z")

</div>

no… it will not…

the problem is, there is no place for you to save the records…  
because the http protocol is no state.

every time when you refresh the page, the variable is gone…

yours,  
Terry

2009/9/9 Venkat E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![venkat\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4af34b/32.png) [@venkat\_ruby](https://www.ruby-forum.com/u/venkat_ruby)\
**Post date:** [September 9, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/pagination-problem/175161/5 "2009-09-09T18:10:38Z")

</div>

On Sep 9, 2:06 am, Venkat E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Please note: I don’t want to use any plugins and please help to  
> implement…

You don’t want to use any plugins? Then you either need to get a new  
job or stop asking rails-talk to do your homework…

Seriously though, mislav-will\_paginate on github does exactly what  
you’re looking for. Don’t re-invent the wheel.

–Matt J.

---

<div class="post-metadata">

**Author:** ![venkat\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4af34b/32.png) [@venkat\_ruby](https://www.ruby-forum.com/u/venkat_ruby)\
**Post date:** [September 9, 2009, 8:53am UTC](https://www.ruby-forum.com/t/pagination-problem/175161/6 "2009-09-09T08:53:24Z")

</div>

Thanks, @Sandip R. @Terry Terry

I have a doubt…

If i write a function in application\_helper.rb( Example: set and get  
variable functions).

It’s possible to implement my requirement…?
