Limit number of pages in will_paginate
Asked Answered
A

3

6

So, when using Sphinx searches are limited to 1000 results. But, if there are more than 1000 results pagination links generated by will_paginate don't take this into account, and provide links to pages beyond 1000/per_page. Is the an obvious way setting a max pages or something similar?

Cheers.

Angelo answered 16/4, 2012 at 11:48 Comment(0)
A
1

I found the best solution was to do this:

@results = Model.search(...)
if @results.total_pages >= (1000/Model.per_page)
  class << @results; def total_pages; 1000/Model.per_page; end end
end

Where the 1000 is preferably not hardcoded :). This gets you the correct behaviour of the will_paginate view helper for free,

Angelo answered 19/4, 2012 at 8:54 Comment(0)
P
13

I think it's better to submit the parameter :total_entries to method paginate:

@posts = Post.paginate(:page => params[:page], :per_page => 30, 
                       :total_entries => 1000)

will_paginate will generate links only for the number of pages needed to show 1000 results.

You can also verify that the requested page belongs to the interval:

if params[:page].to_i * 30 <= 1000
  @posts = Post.paginate(:page => params[:page], :per_page => 30, 
                         :total_entries => 1000)
end

Moreover, by submitting the parameter :total_entries, you avoid the sql COUNT query that will_paginate normally runs to retrieve the total number of entries.

Programme answered 30/11, 2012 at 14:48 Comment(0)
I
2
if params[:page].to_i * 30 <= 1000
  @posts = Post.paginate(:page => params[:page], :per_page => 30)
end
Immunogenic answered 16/4, 2012 at 12:8 Comment(0)
A
1

I found the best solution was to do this:

@results = Model.search(...)
if @results.total_pages >= (1000/Model.per_page)
  class << @results; def total_pages; 1000/Model.per_page; end end
end

Where the 1000 is preferably not hardcoded :). This gets you the correct behaviour of the will_paginate view helper for free,

Angelo answered 19/4, 2012 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.