Rails:防止will_paginate调用ActiveRelation的#count [英] Rails: Prevent will_paginate from calling #count of ActiveRelation
本文介绍了Rails:防止will_paginate调用ActiveRelation的#count的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我通过 will_paginate
一个ActiveRelation时,它将始终调用其#count方法并访问数据库以查找项目总数。但是此操作需要时间,并且我已经缓存了总数并且准备就绪。我可以将这个预先计算的计数传递给 will_paginate
并阻止它访问数据库吗?
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?
我尝试了:count
选项,但作为选项传递给ActiveRecord:
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)
谢谢! :)
推荐答案
通过:total_entries
传递缓存的计数可解决此问题:
Passing the cached count with :total_entries
solves the problem:
active_relation.paginate(page: 2, per_page: 100, total_entries: total_count)
这篇关于Rails:防止will_paginate调用ActiveRelation的#count的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文