Jason Swett
Jason Swett

Reputation: 45074

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?

Upvotes: 1

Views: 340

Answers (2)

Long Nguyen
Long Nguyen

Reputation: 373

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

Upvotes: 0

eugen
eugen

Reputation: 9226

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.

Upvotes: 3

Related Questions