简体   繁体   English

will_paginate宝石不起作用

[英]will_paginate gem not working

I'm having a problem with will_paginate that's pretty simple to express: 我遇到了will_paginate的问题,表达起来非常简单:

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

Obviously, that number should be 10, not 20. 显然,这个数字应该是10,而不是20。

Am I somehow doing something wrong? 我在某种程度上做错了什么? Doesn't seem like there's much room for error here, but obviously something isn't right. 看起来似乎没有太大的错误空间,但显然有些事情是不对的。 What am I missing? 我错过了什么?

You want Story.paginate(page: 1, per_page:10).size , not count . 你想要Story.paginate(page: 1, per_page:10).size ,不count Count will actually return the total numbers of records generated by your query. Count实际上将返回查询生成的记录总数。

Because will_paginate gem overrides the count method 因为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

It will exculde order, limit, offset and reorder . 它将排除order, limit, offset and reorder link to code 链接到代码

You should use size or total_entries 您应该使用sizetotal_entries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM