Rails: Prevent will_paginate from calling #count of ActiveRelation

不羁岁月 提交于 2020-01-14 07:59:12

问题


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! :)


回答1:


Passing the cached count with :total_entries solves the problem:

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


来源:https://stackoverflow.com/questions/26696731/rails-prevent-will-paginate-from-calling-count-of-activerelation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!