Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get page count using Kaminari

Tags:

kaminari

I am iterating through a large array of model objects and need to paginate for performance/memory reasons.

I want to do something like this:

for i in 1..Person.num_pages
  Person.page(i).each do
    # work
  end
end

Obviously I can get the count and do the math myself, but is there an equivalent of num_pages? Or is there a more elegant way of doing this altogether?

like image 718
keithgaputis Avatar asked May 04 '12 13:05

keithgaputis


1 Answers

Use total_pages.

Yes, you can use total_pages to retrieve the total number of pages in your model.

For example:

@blog_entries = Blog.all.page(1).per(20)
puts "total pages: #{@blog_entries.total_pages}"

Version < 0.14.0

Previous to version 0.14.0, the method was called num_pages instead of total_pages.

like image 122
Edmar Miyake Avatar answered Oct 20 '22 23:10

Edmar Miyake