# Pagination with HABTM relationship

**URL:** <https://www.ruby-forum.com/t/pagination-with-habtm-relationship/79862>\
**Category:** Rails\
**Created:** [December 19, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/pagination-with-habtm-relationship/79862 "2006-12-19T23:48:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 19, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/pagination-with-habtm-relationship/79862/1 "2006-12-19T23:48:33Z")

</div>

I am wondering how to do pagination on a model that has a HABTM  
relationship through a join model.

## The models

User  
has\_many :resources  
has\_many :pages, :through =\> :resources  
…

Page  
has\_many :resources  
has\_many :users, :through =\> :resources  
…

Resource  
belongs\_to :user  
belongs\_to :page  
…

Then in the PageController I have a list method:  
def list  
@pages = @user.pages # @user is set with a before\_filter

end

This works fine - but I want to be able to paginate the @pages list

So how do I do this? I have read all that I can find regarding  
pagination and still am not getting it.

Code is always helpful.

Thanks in advance – K

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 20, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/pagination-with-habtm-relationship/79862/2 "2006-12-20T19:16:30Z")

</div>

Is this impossible or just not challenging enough to get a responce?  
Please enlighten me if you know how to do this.

Thanks in advance – K

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 20, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/pagination-with-habtm-relationship/79862/3 "2006-12-20T20:15:26Z")

</div>

Kim,

Here are 2 links to plug-ins that others have recommended. I tried the  
first one about a month ago and never quite got it working (I can’t  
remember what the problem was).

> **[Welcome cardboardrocket.com - BlueHost.com](http://cardboardrocket.com/pages/paginating_find)**
>
> Bluehost - Top rated web hosting provider - Free 1 click installs For blogs, shopping carts, and more. Get a free domain name, real NON-outsourced 24/7 support, and superior speed. web hosting provider php hosting cheap web hosting, Web hosting,...

[http://codefluency.com/2006/10/24/paginator-released](http://codefluency.com/2006/10/24/paginator-released)

-Paul

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 20, 2006, 8:31pm UTC](https://www.ruby-forum.com/t/pagination-with-habtm-relationship/79862/4 "2006-12-20T20:31:36Z")

</div>

On Dec 19, 5:47 pm, “Kim” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Page  
> def list
> 
> Thanks in advance – K

Is the “Custom Pagination” section here helpful?

[http://wiki.rubyonrails.org/rails/pages/HowtoPagination](http://wiki.rubyonrails.org/rails/pages/HowtoPagination)

Best,

-r

–  
Ryan R.  
[http://raaum.org](http://raaum.org)  
[http://rails.raaum.org](http://rails.raaum.org) – Rails docs  
[http://locomotive.raaum.org](http://locomotive.raaum.org) – Self contained Rails for Mac OS X

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 20, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/pagination-with-habtm-relationship/79862/5 "2006-12-20T20:41:38Z")

</div>

The cardboardrocket paginating\_find works great for me. Although I had  
to make some small changes to get it to work properly… here’s an  
example from my code:

@products = Product.find(:all,  
:conditions =\> [“category\_id = ?”, params[:id]],  
:order =\> “name”,  
:page =\> {:size =\> 20,  
:first =\> 1,  
:current =\> params[:page] || 1 })

Notice the way I handle the :current. anyways hope it helps if you go  
that route.

good luck,  
Chad

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 20, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/pagination-with-habtm-relationship/79862/6 "2006-12-20T23:02:31Z")

</div>

Chad - that sounds good. Would you mind sharing a code snippet from  
your view as to how you handle the paging? I think that may have been  
what I was missing.

Thanks,  
Paul

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 20, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/pagination-with-habtm-relationship/79862/7 "2006-12-20T23:46:48Z")

</div>

Thank you Ryan! The custom pagination worked perfectly.

Here is the code I added to the list method in the controller:

# step 1: read and set the variables you’ll need

```
   page = (params[:page] ||= 1).to_i
   items_per_page = 8
   offset = (page - 1) * items_per_page

```

# step 2: do your custom find without doing any kind of limits or

offsets  
@pages = @user.pages

# step 3: create a Paginator, the second variable has to be the

number of ALL items on all pages  
@which\_pages = Paginator.new(self, @pages.length,  
items\_per\_page, page)

# step 4: only send a subset of @pages to the view

```
@pages = @pages[offset..(offset + items_per_page - 1)]
```

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 21, 2006, 12:34am UTC](https://www.ruby-forum.com/t/pagination-with-habtm-relationship/79862/8 "2006-12-21T00:34:55Z")

</div>

On Dec 20, 5:45 pm, “Kim” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @pages = @user.pages
> 
> ```
> 
> # step 3: create a Paginator, the second variable has to be the
> 
> number of ALL items on all pages  
> @which\_pages = Paginator.new(self, @pages.length,  
> items\_per\_page, page)
> 
> # step 4: only send a subset of @pages to the view
> 
> ```
> @pages = @pages[offset..(offset + items_per_page - 1)]
> 
> ```

Mind you, this is not a super efficient method, but as long as the  
number of user.pages is not too large it should not impact performance.  
Now if users have thousands of pages …

Best,

-r

–  
Ryan R.  
[http://raaum.org](http://raaum.org)  
[http://rails.raaum.org](http://rails.raaum.org) – Rails docs  
[http://locomotive.raaum.org](http://locomotive.raaum.org) – Self contained Rails for Mac OS X
