Classic pagination with custom finders/sql


#1

Say I have a user model with the following

def active
find(:all, :conditions => {:state => ‘active’})
end

How can I get this working with classic pagination in my controller
without duplicating my “logic” for finding active users. ie, i do NOT
want to do this

def list
@users = paginate(:users, :conditions => {:state => ‘active’})
end

Is there anyway I can ‘pull’ the conditions out of the finder method?


#2

On Feb 12, 5:31 pm, Ball B. removed_email_address@domain.invalid
wrote:

def list
@users = paginate(:users, :conditions => {:state => ‘active’})
end

Is there anyway I can ‘pull’ the conditions out of the finder method?

If you’re on a recent version of Rails, it would be best to replace
the ‘active’ method above with a named scope:

named_scope :active, :conditions => { :state => ‘active’ }

Then you can use User.active as a base to find only active users. Note
that the scope supports all the
various find methods, ie:

User.active.find(:all, :order => ‘last_name’, :limit => 5)

will work. See the docs at
http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html#M002120


However, on looking at the classic_pagination docs, it may be a bit of
a hassle to create the paginator.
With the new will_paginate, your controller will only need:

def list
@users = User.active.paginate params[:page] || 1 # other options as
needed
end

You may want to consider upgrading…

–Matt J.