Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Item's page and will_paginate

I have some photos that are split on successive pages through will_paginate plugin. When you open a photo and then return to all photos using a link, you always return to the first page (e.g. the photo is displayed on page 5, you open the photo, click a link to show all photos and expect that you are on page 5 again, but you are on page 1).

Now, is there any method to get the page number to which a photo belongs to?

I tried to pass a GET parameter, but this only works if the user doesn't perform any more actions (e.g. post a comment, edit photo, ecc.).

like image 376
collimarco Avatar asked Jun 30 '09 18:06

collimarco


1 Answers

page = (number_of_records_before_RECORD / number_of_records_per_page) + 1

In other words. If your photo has ID 40 and there are 25 records before (assuming some records have been deleted), with 20 records per page:

page = (25 / 20) + 1 = 2

You can count the number of records before the selected record using Model.count(:conditions => ['id < ?', record.id], :order => 'id'). The right query depends on which sorting filter you apply to that table when listing all objects.

like image 79
Simone Carletti Avatar answered Oct 15 '22 06:10

Simone Carletti