Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 13949

Mongoid - Detect if there are results on the next page

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?

Upvotes: 0

Views: 36

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230366

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

Upvotes: 1

Related Questions