Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

The :total_entries option has no effect on paginate. #316

Closed
chunpan opened this Issue · 3 comments

2 participants

@chunpan

My project is using the latest 3.0.4 release on Rails 3.2.12. I tried paginate method from both an Array and an ActiveRecord, none of which seemed to work with :total_entries option. For the ActiveRecord.paginate, I can see the SELECT COUNT(*) from the log even when :total_entries value is given. For the Array version, it is also the case with the following example: [1,2,3,4,5,6,7,8,9,10].paginate(page: 1, per_page: 6, total_entries: 4) - the result is still the first 6 elements.

@mislav
Owner

If you do MyModel.paginate(:page => 1, :per_page => 10, :total_entries => 12).total_entries, do you get "12" as result, or the true number of MyModel entries? Does the COUNT(*) get triggered in that case?

From my testing, it appears to avoid the count. So, that must mean some method that you call on the paginated Relation triggers the COUNT(*), possibly the size method. Can you try locating it?

@chunpan

If I use MyModel.paginate(:page => 1, :per_page => 10, :total_entries => 12).total_entries, I do get "12" without any COUNT() triggered. I think the COUNT() was triggered by an .empty? call we used to check in a while loop. We'll switch to use .total_pages for the while loop in this case.
One question on the ActiveRecord pagination is that (using the same example above) if I get :page => 2 with the same :total_entries of 12, I still get 10 objects on page 2, instead of just 2 (provided that I do have more than 20 records). So the last page doesn't seem to care about this limiting factor, which is probably only used to calculate total_pages. Is this designed as is? If so, that explains why the Array version of paginate also returns the full page of objects, even though the .total_entries and .total_pages are correct. Furthermore, if I ask for :page => 3, it doesn't seem to care about the effective .total_pages. I would expect an exception or empty result in this case.
BTW, thanks for the quick response and insight! Very much appreciate it. :)

@mislav
Owner

So the last page doesn't seem to care about this limiting factor, which is probably only used to calculate total_pages. Is this designed as is?

Yes, total_entries is only an override used to avoid the COUNT query, but doesn't affect the results returned from both SQL queries or Array pagination.

Working with AR Relation and counting is ridiculously tricky. I don't know why size or empty? trigger a COUNT but I recall that I had to make some hack to work around AR edge cases in various versions of Rails. Anyone is welcome to dive in the current implementation and try to reduce the number of hacks.

@chunpan chunpan closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.