Limit the number of pages in will_paginate

Thus, when using Sphinx search, it is limited to 1000 results. But if more than 1000 results are paginated links generated using will_paginate, do not take this into account and provide links to pages outside 1000/per_page. Is the obvious way to set max pages or something like that?

Greetings.

+5
source share
3 answers

I found a better solution for this:

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

If 1000 is preferably not hardcoded :). This gives you the correct view helper behavior will_paginate for free,

+1
source

I think it is better to pass the parameter to the :total_entriesmethod paginate:

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

will_paginate , 1000 .

, :

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

, :total_entries, sql COUNT, , .

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

All Articles