Will Paginate Rails 3 Per Page
Asked Answered
P

3

9

I am trying to limit the number of elements returned with mislav's will paginate with Rails 3. I am currently using:

# Gemfile
gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git', :branch => 'rails3'

# company.rb
class Company < ActiveRecord::Base
  self.per_page = 8
end

# company_controller.rb
def index
  @companies = Company.where(...).paginate(:page => params[:page])
end

This does pagination, but not 8 items per page. If I modify the code to not use the "where" it works fine. However, adding "where" or "scoped" seems to cause issues. Any ideas what I'm doing wrong?

Thanks.

Pulsatory answered 22/6, 2010 at 17:56 Comment(0)
P
14

Ended up being forced to move the per page limit into the query. Appears to be a bug with the Rails 3 version. Thus, fixed using:

@companies = Company.where(...).paginate(:page => params[:page], :per_page => 8)
Pulsatory answered 5/7, 2010 at 4:50 Comment(1)
This was a bug. Now fixed! Use gem 'will_paginate', '~> 3.0.pre4'Kinzer
P
4

@Kevin, if you want to be sure per_page is consistent across various queries you can use Company.per_page, eg.

@companies = Company.where(...).paginate(:page => params[:page], :per_page => Company.per_page)

You may also give a try to Kaminari gem which is much better integrated with rails 3: http://railscasts.com/episodes/254-pagination-with-kaminari

class Company < ActiveRecord::Base
  paginates_per 7
end

@companies = Company.where(...).page(params[:page])
Pucka answered 21/1, 2011 at 16:22 Comment(0)
G
0

Why are you using 'Companies' and not 'Company'. This might just be a typo here but it appears to be an issue.

Gombosi answered 6/7, 2010 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.