Rails: Prevent will_paginate from calling #count of ActiveRelation
Asked Answered
B

1

8

When I pass will_paginate an ActiveRelation, it always calls its #count method and hits the database to find out the total number of items. But this operation takes time and I have the total number already cached and ready. Can I pass this pre-calculated count to will_paginate and stop it from hitting the database?

I tried the :count option, but it is passed to ActiveRecord as an option:

active_relation.paginate(page: 2, per_page: 100, count: total_count)

Thanks! :)

Bandstand answered 2/11, 2014 at 6:40 Comment(0)
B
12

Passing the cached count with :total_entries solves the problem:

active_relation.paginate(page: 2, per_page: 100, total_entries: total_count)
Bandstand answered 2/11, 2014 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.