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.
source share