will_paginate with named_scopes will_paginate with named_scopes ruby-on-rails ruby-on-rails

will_paginate with named_scopes


Lowgain, klew's version should work out of the box. In your version you should write:

User.scope.paginate :page => params[:page], :per_page => 10

I prefer another approach to pagination. It allows to make controller more clean and encapsulates pagination at model level, for e.g.:

class Property < ActiveRecord::Base  named_scope :all_properties, lambda {{ :order => "name asc" }}  def self.admin_properties(page = 1)    self.all_properties.paginate(:page => page, :per_page => Settings.admin.per_page)  endend

And in a controller pretty clear code:

class Admin::PropertiesController < Admin::AdminController  def index    @properties = Property.admin_properties(params[:page])  endend

p.s: Settings.admin.per_page - this is Searchlogic settings.


I have a named_scope defined like this:

 named_scope :look_for, lambda { |search| bla;bla;bla }

I call it:

 Person.look_for(params[:search]).paginate :page => params[:page]

And it works. Maybe you need some parameter to your scope?


Kind of a weird solution, but

User.scope.find(:all).paginate :page => params[:page], :per_page => 10

works?