will_paginate on complex scopes may be slow (workaround)

Posted . Visible to the public.

will_paginate Show archive.org snapshot triggers a database query to determine the total number of entries (i.e. to let you display the number of search results). When you paginate complex scope (e.g. that has many includes), this query may take several seconds to complete.

If you encounter this behavior, a solution is to calculate the total count yourself and pass it to the pagination call:

scope = User.complex_scope_full_of_includes
total_number_of_users = scope.count
@users = scope.paginate(:total_entries => total_number_of_users)
Dominik Schöler
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2012-02-23 14:36)