Pagination problem

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…

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. [email protected]:

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 [email protected] wrote:

Hi all,

variable.


Ruby on Rails Developer

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. [email protected]:

On Sep 9, 2:06 am, Venkat E. [email protected]
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.

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…?