simonmorley
simonmorley

Reputation: 2804

Paginate with find_each in active record

I've got a huge set of records to display and have broken the query down using find_each:

 - Syslog.find_each(:batch_size => 2000) do |s|
   = s.something

The problem is trying to introduce pagination. I've tried will_paginate (3.0.3):

 - Syslog.paginate(:page => params[:page], :per_page => 10).find_each(:conditions => {  }, :batch_size => 2000 ) do |s|

Is there a way to paginate a large set of records like this?

Upvotes: 0

Views: 1512

Answers (2)

Prem
Prem

Reputation: 5964

Try to fire query using where on paginate as below

Syslog.paginate(:page => params[:page], :per_page =>10).where(conditions: "your condition")

That should work :)

Upvotes: 1

simonmorley
simonmorley

Reputation: 2804

Using will_paginate 3.0.3, I found the actual solution was to do this:

Model.accessible_by(current_ability).page(params[:page]).order('id DESC')  

Found that on the will_paginate wiki.

Using that, I don't need find_each or batch size and have hundreds of thousands of records loading in a snap.

Thanks to @prem for his initial help. Plus one for assisting.

Upvotes: 0

Related Questions