# Pagination: getting item/attributes from prev/next page

**URL:** <https://www.ruby-forum.com/t/pagination-getting-item-attributes-from-prev-next-page/225890>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/pagination-getting-item-attributes-from-prev-next-page/225890 "2005-11-12T12:49:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/pagination-getting-item-attributes-from-prev-next-page/225890/1 "2005-11-12T12:49:16Z")

</div>

Hi all,

Is it possible to get the item(s) from previous or next pages in a  
Paginator?

I have a 1-item-per-page Paginator, and I need to access the  
attributes of the object in the previous and next pages.

Let me give my code to better illustrate the situation. In my  
controller I have this:

def photo  
…  
@album = Album.find(:first,  
:conditions =\> [‘slug = ?’, params[:albumslug]])  
@photo = Photo.find(:first,  
:conditions =\> [‘album\_id = ? and slug = ?’,  
@album.id, params[:photoslug]])  
@photo\_pages, @photos =  
paginate :photos,  
:order\_by =\> “order\_rank asc”,  
:per\_page =\> 1,  
:conditions =\> [“status=‘published’ and album\_id = ?”,  
@album.id]  
@prevphoto = ??? # what to put here?  
@nextphoto = ??? # what to put here?  
…  
end

And in my view photo.rhtml I have something like this:

…  
\<% if @prevphoto %\>  
\<%= link\_to image\_tag(@prevphoto.src\_uri,  
:alt =\> h(@prevphoto.title),  
:title =\> h(@prevphoto.title)),  
photo\_url(:albumslug =\> @prevphoto.album.slug,  
:photoslug =\> @prevphoto.slug) %\>  
\<% end %\>  
…

And similarly for @nextphoto.

Thanks in advance.

Cheers,  
Ronny

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/pagination-getting-item-attributes-from-prev-next-page/225890/2 "2005-11-12T12:49:19Z")

</div>

On Nov 7, 2005, at 11:40 PM, Ronny H. wrote:

> ```
> :per_page => 1,
> 
> ```
> 
> \<% if @prevphoto %\>  
> \<%= link\_to image\_tag(@prevphoto.src\_uri,  
> :alt =\> h(@prevphoto.title),  
> :title =\> h(@prevphoto.title)),  
> photo\_url(:albumslug =\> @prevphoto.album.slug,  
> :photoslug =\> @prevphoto.slug) %\>  
> \<% end %\>  
> …

Ronny-

```
Just an idea but why don't you paginate by 3 items and then just

```

show the middle one? This way you have access to the previous and  
next items in the pagination but you only need to show the middle one  
so it looks like you are paging by one. This is the simplest way I  
can think to solve this issue.

HTH

-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/pagination-getting-item-attributes-from-prev-next-page/225890/3 "2005-11-12T12:49:28Z")

</div>

On Tue, Nov 08, 2005 at 08:30:13AM -0800, Ezra Z. wrote:

> show the middle one? This way you have access to the previous and  
> next items in the pagination but you only need to show the middle one  
> so it looks like you are paging by one. This is the simplest way I  
> can think to solve this issue.

G’day Ezra,

I had actually thought about paginating by 3 items, but it’s not the  
most natural/intuitive way, I’m afraid it would make code maintenance  
a wee bit more difficult in the future.

Having said that, paginating by 3 items should work fine. Need to  
handle first and last pages as they are special cases. The middle  
pages should be easy enough to figure out.

I will do it this way for now. I’m still open for other suggestions,  
though.

Thank you for your help.

Ronny
