I have a system that produces literally thousands of reports in a single request to populate a table and I use kamanari to paginate 20 records per page. Because there could be thousands of reports being loaded in a single request, it sometimes takes several minutes for them to load, regardless of the request format is html or json. I tried a number of things with kaminari.
my_controller.rb
def my_table
reports = unit.reports_for(history_date_range) #This could return possibly 10,000 reports
paginated_reports = reports.order('time desc').page(params[:page]).per(20)
@reports = paginated_reports
...
end
my_table.html.haml
- @reports.each do |r|
...
= paginate @reports.page(params[:page]).per(20)
The problem with the above is that kaminari doesn't send only 20 records back to the view. It sends all 10,000, for example. And just paginates them in groups of 20. Even when I click the second page, it goes through the whole pagination route again, and queries 10,000 records and shows 20 records on the page.
Rather than this:
= paginate @reports.page(params[:page]).per(20)
I also tried this:
= paginate @reports, :total_pages => 2
And this still takes long but only shows 2 total pages, not all the pages that there are.
What I am trying to do is as follows. Let's say there are 100 records total and that means there are 50 pages in groups of 20. I only want to query the first 20, send it back to the view, and populate the view with those 20 records and have a little arrow sign indicating that there are more. Then when the user clicks that arrow sign, it then queries only the next set of 20 records, sends them back to view, and so forth. I don't want to have to be querying so many records at a single time and have the entire view populate with them.