简体   繁体   中英

will_paginate gem not working

I'm having a problem with will_paginate that's pretty simple to express:

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

Obviously, that number should be 10, not 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 . Count will actually return the total numbers of records generated by your query.

Because will_paginate gem overrides the count method

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 . link to code

You should use size or total_entries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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