will_paginate gem 不工作

will_paginate gem not working

我遇到了 will_paginate 的问题,表达起来很简单:

> Story.paginate(page: 1, per_page: 10).count
=> 20

显然,这个数字应该是 10,而不是 20。

我是不是做错了什么?这里似乎没有太多出错的余地,但显然有些地方不对。我错过了什么?

你想要 Story.paginate(page: 1, per_page:10).size,而不是 count。计数实际上 return 您的查询生成的记录总数。

因为will_paginate gem 覆盖了count 方法

def count(*args)
  if limit_value
    excluded = [:order, :limit, :offset, :reorder]
    excluded << :includes unless eager_loading?
    rel = self.except(*excluded)
    column_name = (select_for_count(rel) || :all)
    rel.count(column_name)
  else
    super(*args)
  end
end

会排除order, limit, offset and reorderlink to code

您应该使用 sizetotal_entries