# Custom pagination

**URL:** <https://www.ruby-forum.com/t/custom-pagination/58638>\
**Category:** Rails\
**Created:** [April 24, 2006, 11:56am UTC](https://www.ruby-forum.com/t/custom-pagination/58638 "2006-04-24T11:56:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 24, 2006, 11:56am UTC](https://www.ruby-forum.com/t/custom-pagination/58638/1 "2006-04-24T11:56:38Z")

</div>

I am trying to paginate objects from a has\_many/:through relationship  
using Paginator.

current\_user.things returns the objects that I’d like to paginate. For  
testing, I tried to make it 1 item per page. I initialise the paginator  
as follows:

@thing\_pages = Paginator.new(self, current\_user.things.count, 1,  
@params[‘page’])

I don’t know how to obtain @things now though, I don’t want to be using  
Things.find\_all (as the wiki example shows) as I know which things I  
want to be paginating (current\_user.things). How can I make an array of  
@things that contains the right items (e.g. starts and ends at the  
correct offset)?

@things = current\_user.things will obviously just return all things,  
which isn’t correct. Somehow I need to select only the correct portion  
of this?

Thanks.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 24, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/custom-pagination/58638/2 "2006-04-24T14:15:09Z")

</div>

David wrote:

> @things = current\_user.things will obviously just return all things,  
> which isn’t correct. Somehow I need to select only the correct portion  
> of this?

Whilst browsing the Typo source I found the answer

```
@things = current_user.things
@thing_pages = Paginator.new(self, @things.size, 1, @params['page'])
start = @thing_pages.current.offset
stop = (@thing_pages.current.next.offset - 1) rescue things.size
@things = @things.slice(start..stop)

```

I especially like the rescue condition in the stop, a very neat way of  
fixing the last page.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 24, 2006, 3:08pm UTC](https://www.ruby-forum.com/t/custom-pagination/58638/3 "2006-04-24T15:08:29Z")

</div>

The other way to do this is by passing the correct information to find.

@things = Things.find(:all, :offset=\>@thing\_pages.current.offset,  
:limit=\>@thing\_pages.items\_per\_page)

On Monday, April 24, 2006, at 2:15 PM, David wrote:

> stop = (@thing\_pages.current.next.offset - 1) rescue things.size  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> \_Kevin

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 24, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/custom-pagination/58638/4 "2006-04-24T15:32:10Z")

</div>

Kevin O. wrote:

> The other way to do this is by passing the correct information to find.
> 
> @things = Things.find(:all, :offset=\>@thing\_pages.current.offset,  
> :limit=\>@thing\_pages.items\_per\_page)

However, surely this will return all Things, rather than just the ones  
owned by the current user? That was the one thing I was trying to fix  
here.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 24, 2006, 4:10pm UTC](https://www.ruby-forum.com/t/custom-pagination/58638/5 "2006-04-24T16:10:40Z")

</div>

Oh, yes.

add :conditions=\>“user\_id = #{current\_user.id}”

or use this…

@things = current\_user.things(:offset=\>@thing\_pages.current.offset,  
:limit=\>@thing\_pages.items\_per\_page)

I think the second one will work, but I haven’t tried it.

Bottom line, just add the appropriate offest and limit parameters to  
whichever find function you use.

On Monday, April 24, 2006, at 3:32 PM, David wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

\_Kevin

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 24, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/custom-pagination/58638/6 "2006-04-24T17:07:39Z")

</div>

Kevin O. wrote:

> Oh, yes.
> 
> add :conditions=\>“user\_id = #{current\_user.id}”

Unfortunately I can’t do this, as I’m using a has\_many/:through  
relationship so am not using a find() call. There is no “user\_id”  
column, that’s handled by a permissions table.

> or use this…
> 
> @things = current\_user.things(:offset=\>@thing\_pages.current.offset,  
> :limit=\>@thing\_pages.items\_per\_page)

This looks promising, but I can’t get it to work. As I already have  
current\_user.things, I can’t query using the class find\*() methods (for  
the same reason as above). I also can’t find any methods for the  
array/collection that allow an offset and limit to be passed to them.

Since posting I’ve actually found this link:  
[BigBold - Informasi Tentang Bisnis dan Marketing](http://www.bigbold.com/snippets/posts/show/389), which solves the problem  
in pretty much the same way as Typo (and therefore the code I borrowed).

In this instance, I think I’ll have to make do with a few lines extra 🙂

Cheers.
