# \[PLUGIN\] ArPaginator - Allows you to easily paginate over an

**URL:** <https://www.ruby-forum.com/t/plugin-arpaginator-allows-you-to-easily-paginate-over-an/91064>\
**Category:** Rails\
**Created:** [April 4, 2007, 7:22am UTC](https://www.ruby-forum.com/t/plugin-arpaginator-allows-you-to-easily-paginate-over-an/91064 "2007-04-04T07:22:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 4, 2007, 7:22am UTC](https://www.ruby-forum.com/t/plugin-arpaginator-allows-you-to-easily-paginate-over-an/91064/1 "2007-04-04T07:22:43Z")

</div>

The built-in Rails pagination is fairly limited…you pass in the  
model name, and optionally some conditions, and it generates a  
paginated query for you. This gets to be very bad if you’ve got any  
custom queries.

class Company \< ActiveRecord::Base  
def complete\_videos  
Video.find :all, :conditions =\> “company\_id=#{id} AND  
status=‘complete’”, :order =\> “created\_at DESC”  
end  
end

You wouldn’t be able to easily paginate over that query. You’d end up  
doing something like this in your controller:

@video\_pages, @videos = paginate :users, :conditions =\>  
“company\_id=#{@company.id} AND status=‘complete’”, :order =\>  
“created\_at DESC”

that’s obviously not DRY. You’ll have to update your paginate call if  
the logic of complete\_videos changes, and on top of that you’re  
putting busines logic in the controller anyway.

ArPaginator solves this problem by letting you paginate over any  
query. Using it, the new pagination code would be

paginator = ArPaginator.new(:model =\> :video, :controller =\> :videos)  
@video\_pages, @videos = paginator.paginate(:page =\> params[:page]) {  
@company.complete\_videos }

Now you can take any query that returns an array and paginate it easily.

ArPaginator.new takes a hash with two keys - :model and :controller.  
:model is the underscored class name to be paginated. So in this case  
AR ultimately calls Video.find, so we need to specify :video as the  
model. The :controller option is used to specify the controller so the  
call to

pagination\_links @video\_pages

works fine.

The API is a bit messy, and I’ll be cleaning it up soon  
hopefully…but the important part - easily paginating your existing  
query methods - is taken care of.

One limitation is that you can’t paginate over methods that use the  
association proxy. So if your method was

class Company \< ActiveRecord::Base  
has\_many :videos

def complete\_videos  
videos.find :all, :conditions =\> “status=‘complete’”, :order =\>  
“created\_at DESC”  
end  
end

then the pagination would fail. This is because Rails seems to ignore  
the :limit option when using with\_scope around an association proxy.  
You would have to convert that code to not use the association proxy,  
as in the first example.

This plugin requires the count\_from\_query plugin

ruby script/plugin install  
svn://evang.eli.st/public/plugins/count\_from\_query  
ruby script/plugin install  
svn://evang.eli.st/public/plugins/ar\_paginator

Released under the MIT License

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 4, 2007, 9:23am UTC](https://www.ruby-forum.com/t/plugin-arpaginator-allows-you-to-easily-paginate-over-an/91064/2 "2007-04-04T09:23:55Z")

</div>

Pat M. wrote:

> end  
> putting busines logic in the controller anyway.  
> ArPaginator.new takes a hash with two keys - :model and :controller.  
> hopefully…but the important part - easily paginating your existing  
> “created\_at DESC”  
> ruby script/plugin install svn://evang.eli.st/public/plugins/count\_from\_query  
> ruby script/plugin install svn://evang.eli.st/public/plugins/ar\_paginator
> 
> Released under the MIT License
> 
> >

Elegant usage of your previous plugin 🙂 Very sweet.

G

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 4, 2007, 9:53am UTC](https://www.ruby-forum.com/t/plugin-arpaginator-allows-you-to-easily-paginate-over-an/91064/3 "2007-04-04T09:53:45Z")

</div>

On 4/4/07, Gustav P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > status=‘complete’", :order =\> “created\_at DESC”  
> > that’s obviously not DRY. You’ll have to update your paginate call if  
> > Now you can take any query that returns an array and paginate it easily.
> > 
> > def complete\_videos  
> > This plugin requires the count\_from\_query plugin
> 
> G

Actually, the count\_from\_query plugin was an extraction from this one.

The middle of the paginate method had all the code that you see in  
CFQ. I realized that that functionality would be generally useful as  
well.

But yeah, thanks 🙂

Pat
