zargony.com

#![desc = "Random thoughts of a software engineer"]

Paginating special queries with HasFinder and will_paginate

Trying to paginate special queries gave me a headache for quite some time. As summarized in one of my previous blog posts "Paginating special queries", custom finder methods do not work without problems if you use the will_paginate plugin.

There are several plugins that make life easier when you need custom finder methods to do special queries in your models. Widely known are scope_out and scoped_proxy. Both of them are great plugins with different features, but unfortunately neither of them work out of the box with pagination.

Some days ago, Nick of Pivotal Blabs brought back to my mind, that there's another plugin called HasFinder. HasFinder basically works like scopeout and scopedproxy and combines advantages of both plugins. Besides the basic idea to define different scopes for an ActiveRecord model, you can also access methods (finder) in nested scopes that exactly behave like ActiveRecord associations do. Additionally, HasFinder works out of the box with will_paginate.

Head over to Nick's HasFinder article to find out more about it. HasFinder is available as a gem or can be installed as a plugin from SVN. Great plugin, Nick.

Update 2008-04-02: HasFinder functionality will be included in Rails 2.1, called named_scope