# Pagination problem

**URL:** <https://www.ruby-forum.com/t/pagination-problem/173914>\
**Category:** Rails\
**Created:** [August 18, 2009, 8:23am UTC](https://www.ruby-forum.com/t/pagination-problem/173914 "2009-08-18T08:23:53Z")\
**Posts on this page:** 5\
**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:** [August 18, 2009, 8:23am UTC](https://www.ruby-forum.com/t/pagination-problem/173914/1 "2009-08-18T08:23:53Z")

</div>

Hi,  
please help…

In controller…

def index  
@customers = Customer.find(:all)  
pagination code here…  
end

In index.html.erb

Displayes list of customers and has pagination 1,2,3,4…etc.

On clicking page 2, it will again run this(Customer.find(:all)) query in  
controller. So, there will be lack of performance.

So, how to increase the mysql performace? Can i use the index as below  
or any other way is there?

ALTER TABLE customers ADD INDEX(customer\_id);

---

<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:** [August 18, 2009, 8:40am UTC](https://www.ruby-forum.com/t/pagination-problem/173914/2 "2009-08-18T08:40:31Z")

</div>

you have download wii\_paginate and put vendor/plugin then

change environment.rb  
put after end require ‘will\_paginate’  
then in ur controller #@images = Image.paginate :page =\> params[:page],  
:per\_page =\> 2 do some thing like this

and in ur index page \<%= will\_paginate @images%\>

this is ur paging

---

<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:** [August 18, 2009, 9:48am UTC](https://www.ruby-forum.com/t/pagination-problem/173914/3 "2009-08-18T09:48:37Z")

</div>

Wap A. wrote:

> you have download wii\_paginate and put vendor/plugin then
> 
> change environment.rb  
> put after end require ‘will\_paginate’  
> then in ur controller #@images = Image.paginate :page =\> params[:page],  
> :per\_page =\> 2 do some thing like this
> 
> and in ur index page \<%= will\_paginate @images%\>
> 
> this is ur paging

Hi Wap A.,

I am not using any plugins for pagination. I have written my own code  
for the pagination.

So, how to increase the mysql performace? Can i use the index as below  
or any other way is there?  
ALTER TABLE customers ADD INDEX(customer\_id);

---

<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:** [August 18, 2009, 10:35am UTC](https://www.ruby-forum.com/t/pagination-problem/173914/4 "2009-08-18T10:35:05Z")

</div>

if you have written your own pagination then start using :limit option  
in find. I guess you can also use :start

---

<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:** [August 18, 2009, 9:57am UTC](https://www.ruby-forum.com/t/pagination-problem/173914/5 "2009-08-18T09:57:38Z")

</div>

On Aug 18, 3:48 am, Venkat E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > this is ur paging  
> > Posted viahttp://www.ruby-forum.com/.  
> > Is there an actual reason you’re rolling your own code when  
> > will\_paginate handles this prettttty well already?

And of course you need the right indexes in place, but Customer.find  
(:all) kindof defeats the whole purpose of pagination. A non worst-  
possible solution for pagination involves using LIMIT and OFFSET in  
your query. But I’m really curious as to why you’re insistent on re-  
inventing the wheel (honestly, fairly poorly) here.
