0

Using Mongoid, how can I check if given the limit/results_per_page options there is a next page ?

Suppose my scope is

MyCollection.page(@page).per(@per)

Calling .count will compute the total number of results across all pages. But how do I detect if the current page is full/empty ? Or better if the next page is empty ? Do I have to resolve the scope by calling something like to_a?

Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164

1 Answers1

1

Calling .count will compute the total number of results across all pages.

Yes. And you know @page and @per. That's all you need. As long as @page * @per is less than count, you have (at least one) next page. (assuming pages start with 1 and not 0)

Example:

We have 57 elements and our page size is 20.

@page    @per   displaying   @page*@per  count  have next page?
  1       20      1 - 20        20         57         yes
  2       20      21 - 40       40         57         yes
  3       20      41 - 57       60         57         no
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367