计数器缓存和will_paginate

Art*_*rti 1 ruby ruby-on-rails will-paginate

有没有办法强制使用will_paginate插件来使用counter_cache而不是使用 SELECT count(*)查询?我听说Model.count默认使用计数器缓存.所以will_paginate必须使用size方法.我如何改变收集大量分页对象的方式?

小智 6

假设有两个模型,Author并且Comment,在Author has_many Comment使用计数器缓存的情况下,您应该能够将选项传递:total_entriespaginate-method,如下所示:

author = Author.first
posts  = author.posts.paginate :total_entries => author.posts_count
Run Code Online (Sandbox Code Playgroud)

在此处查看更多信息:https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/active_record.rb#L128