# Classic pagination with custom finders/sql

**URL:** <https://www.ruby-forum.com/t/classic-pagination-with-custom-finders-sql/160206>\
**Category:** Rails\
**Created:** [February 12, 2009, 11:31pm UTC](https://www.ruby-forum.com/t/classic-pagination-with-custom-finders-sql/160206 "2009-02-12T23:31:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![balla123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@balla123](https://www.ruby-forum.com/u/balla123)\
**Post date:** [February 12, 2009, 11:31pm UTC](https://www.ruby-forum.com/t/classic-pagination-with-custom-finders-sql/160206/1 "2009-02-12T23:31:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![balla123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@balla123](https://www.ruby-forum.com/u/balla123)\
**Post date:** [February 13, 2009, 8:52pm UTC](https://www.ruby-forum.com/t/classic-pagination-with-custom-finders-sql/160206/2 "2009-02-13T20:52:05Z")

</div>

On Feb 12, 5:31 pm, Ball B. [removed\_email\_address@domain.invalid](mailto: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](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.
