# How to go to the desired page using pagination

**URL:** <https://www.ruby-forum.com/t/how-to-go-to-the-desired-page-using-pagination/137012>\
**Category:** Rails\
**Created:** [May 8, 2008, 9:04am UTC](https://www.ruby-forum.com/t/how-to-go-to-the-desired-page-using-pagination/137012 "2008-05-08T09:04:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [May 8, 2008, 9:04am UTC](https://www.ruby-forum.com/t/how-to-go-to-the-desired-page-using-pagination/137012/1 "2008-05-08T09:04:17Z")

</div>

Hi all,

i used pagination and it is working fine. But i’ve the folloeing  
problem…

- For ex: if i’ve 10 pages and each page contains 10 posts.if i delete  
a post in the 4th page then that post is deleted and the 1st page is  
displayed but i want to display 4th page i,e.; the page where i deleted  
the post should be displayed.  
so how to get this…

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [May 8, 2008, 11:15am UTC](https://www.ruby-forum.com/t/how-to-go-to-the-desired-page-using-pagination/137012/2 "2008-05-08T11:15:38Z")

</div>

it would be helpful to know, how exactly you’re using pagination, since  
there are different ways to do this…

if you use will\_paginate plugin you would have a line like this

@products = Product.paginate(:all, :order =\> “views DESC”, :page =\>  
params[:page], :per\_page =\> params[:per\_page])

there you would simply set :page =\> to whatever you want.

if you find with :limit, you can set :offset to the pagenumber you want  
to display

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [May 8, 2008, 11:51am UTC](https://www.ruby-forum.com/t/how-to-go-to-the-desired-page-using-pagination/137012/3 "2008-05-08T11:51:05Z")

</div>

Thorsten M. wrote:

> it would be helpful to know, how exactly you’re using pagination, since  
> there are different ways to do this…
> 
> if you use will\_paginate plugin you would have a line like this
> 
> @products = Product.paginate(:all, :order =\> “views DESC”, :page =\>  
> params[:page], :per\_page =\> params[:per\_page])
> 
> there you would simply set :page =\> to whatever you want.
> 
> if you find with :limit, you can set :offset to the pagenumber you want  
> to display

Sorry i didn’t mention completely…  
well i’m using a complex query which consists of some 4-5 tables …

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [May 8, 2008, 11:58am UTC](https://www.ruby-forum.com/t/how-to-go-to-the-desired-page-using-pagination/137012/4 "2008-05-08T11:58:25Z")

</div>

> Sorry i didn’t mention completely…  
> well i’m using a complex query which consists of some 4-5 tables …

so how would that complex query lokk like?

you’re using plain SQL?  
then you can give SQL a second param with the LIMIT argument like:  
LIMIT 3, 3  
where the second arg is the pagenumber (like offset in the above  
examples)

or you use find with :include, then the :limit, :offset should still  
work.

otherwise please post your code…

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [May 8, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/how-to-go-to-the-desired-page-using-pagination/137012/5 "2008-05-08T15:51:54Z")

</div>

For deletion this is rather straightforward: you just remember the  
page number and jump back to it (with a check to make sure that page  
still exists after you deleted one)

But if I understand correctly, you want the more general case that  
after an update it should jump to the page where the new/updated  
record is displayed (and perhaps do a yellow flash thing there). I had  
the same problem. Neither the classic, nor will\_paginate can do this  
for you automatically and there is no SQL statement to find the place  
of a record in a table. But if your table is sorted on a unique key  
you can get the new page number by doing a properly constructed “count  
smaller than key” and dividing that by the page length. If it’s not  
sorted on a unique key you have to brute force it: read in the whole  
table (use select on key) to find the place in the list.

On May 8, 9:04 am, Vamsi K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
