How to go to the desired page using pagination

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...

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

Thorsten Mueller 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 ....

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...

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.