2014-11-02 42 views
6

当我通过will_paginate ActiveRelation,它总是调用其#count方法,并击中数据库找出项目的总数。但是这个操作需要时间,而且我已经将总数缓存并准备好了。我可以将这个预先计算的计数传递给will_paginate并阻止其击中数据库吗?Rails:阻止will_paginate调用#的ActiveRelation

我试过:count选择,但它传递给ActiveRecord的作为一个选项:

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

谢谢! :)

回答

10

传递与:total_entries缓存计数解决了这个问题:

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