Forum: Ruby on Rails classic pagination with custom finders/sql

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ball B. (Guest)
on 2009-02-13 00:31
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?
Matt J. (Guest)
on 2009-02-13 21:52
(Received via mailing list)
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/Na...

---

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.
This topic is locked and can not be replied to.