Will_paginate shows the last page by default (keeping chronological order)

I am creating an index page where I need to display results that have a date arranged in chronological order (from the earliest date to the newest), but I need to always show the last page by default.

I cannot use the DESC order for the date, because it will defeat the whole purpose of what I need for for = (.

TL DR: I need to show a list of broken logs, sorted by date in ascending order, but starting from the last page. Is this possible with will_paginate? Thanks!

+4
source share
4 answers

This is the solution I finally came to:

if params[:page] @collection = @q.result(distinct: true).paginate(:page => params[:page], :per_page => 30) else last_page = @q.result(distinct: true).paginate(:page => params[:page], :per_page => 30).total_pages @collection = @q.result(distinct: true).paginate(:page => last_page, :per_page => 30) end 

This is not a fantasy, it needs some refactoring and, perhaps, can use memoization to avoid calling the paginate method twice, but it works. If you try to access the page directly, it appears as usual, but if you do not specify the page, by default you get to the last page.

0
source

You can build your query, get full pages on the first pass, and then extract the last page on the next pass.

 query = Log.some_scope total_pages = query.paginate(:page => 1).total_pages @logs = query.paginate(:page => total_pages) 

You can also try Kaminari gem instead of will_paginate, which makes this a bit more convenient.

+3
source

Try this code:

 if params[:page] @collection = @q.result(distinct: true).paginate(:page => params[:page], :per_page => 30) session[:lastpage] = params[:page] else @collection = @q.result(distinct: true).paginate(:page => session[:lastpage] = params[:page], :per_page => 30) end 
+1
source
 if params[:page] @collection = ordered_query_results.paginate(page: params[:page], per_page: PER_PAGE) else total_results = ordered_query_results.count last_page = total_results / PER_PAGE + (total_results % PER_PAGE == 0 ? 0 : 1) @collection = ordered_query_results.paginate(page: last_page, per_page: PER_PAGE) end 
0
source

Source: https://habr.com/ru/post/1432600/


All Articles