# Redirect to last page in pagination

**URL:** <https://www.ruby-forum.com/t/redirect-to-last-page-in-pagination/81296>\
**Category:** Rails\
**Created:** [January 6, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/redirect-to-last-page-in-pagination/81296 "2007-01-06T23:01:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [January 6, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/redirect-to-last-page-in-pagination/81296/1 "2007-01-06T23:01:25Z")

</div>

Hey,

I have a forum and at the bottom of each page of the current topic I  
have a form to add a reply.

I am using the following pagination to display 20 posts per topic per  
page…

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

  @posts = Post.find(:all, :conditions => ["topic_id = ?",

```

params[:id]], :order =\> “created\_on DESC”)

```
  @post_pages = Paginator.new(self, @posts.length, posts_per_page,

```

page)

```
  @posts = @posts[offset..(offset + posts_per_page - 1)]

```

My question is:

When a user adds a reply, I want to then redirect that person to the  
last page of posts (which is where their reply will have been added)

Any ideas how i could go about this?

Thanks!!

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [January 7, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/redirect-to-last-page-in-pagination/81296/2 "2007-01-07T20:11:04Z")

</div>

Scott H. wrote:

> last page of posts (which is where their reply will have been added)  
> Any ideas how i could go about this?

Pagination is a highly debated topic with several ways of implementing  
it. While it may be better to use one of the pagination plugins, here  
is a suggestion based on your current implementation.

To get the number of total posts, use: Posts.count - your current code  
selects every single post from the database on a request to that  
action, which is inefficient and will not scale.

You could support a custom ?page=last parameter.

total = Post.count  
limit = 20  
page = params[:page] || 1  
page = (total.to\_f / limit).ceil if page == “last”

@posts = Topic.find(params[:id]).posts.find(all, :limit =\> limit,  
:offset =\> (page.to\_i-1)\*limit, :order =\> “created\_on DESC”)  
@post\_pages = Paginator.new(self, total, limit, page.to\_i)

I’m not sure this is the cleanest way to implement what you want, but  
it will work.

redirect\_to :controller =\> ‘posts’, :action =\> ‘list’, :page =\> ‘last’

Dan M.

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [January 9, 2007, 12:48pm UTC](https://www.ruby-forum.com/t/redirect-to-last-page-in-pagination/81296/3 "2007-01-09T12:48:23Z")

</div>

Hey,

Thanks for that. I’ll give it a go.

I never actually thought about pagination plugins. Are there any you  
would recommended?

Scott

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [January 9, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/redirect-to-last-page-in-pagination/81296/4 "2007-01-09T14:47:01Z")

</div>

question about pagination and how it does not scale well, what size  
recordset will pagination start being a problem? \< 1000, \> 2000, a few  
hundred? or are we talking 10k and above?
