Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails will_paginate and ajax, paging while the DB table changes

I'm trying to use will_paginate for a Rails 3.2 app and I didn't see any reference anywhere for what happens when someone is paging while the data in the db is changed. More specifically, new records are added.

I might be missing something here, but if I get how it works, will_paginate simply counts the records in the DB. Say I load a page with 5 records at a time and want them ordered by the newest first. A user loads the page and gets records 6-10 (the newest records at that time) showing at page 1. Then, someone inserts another record to the table, id=11. After that, the first user clicks to get to page 2 but now page 2 gives records 2-6. So the user got id=6 in both pages.

This problem doesn't sound that bad, but it is really bad if you want to use will_paginate paging for an infinite scroll as shown here: http://railscasts.com/episodes/114-endless-page

I thought about adding a time stamp of the first page load and pass it to consecutive ajax calls to filter on the records that were present at first in order to not get the duplicates.

Is there some best practice way to handle this?

like image 559
Oded Avatar asked Nov 14 '22 01:11

Oded


1 Answers

Your solution of checking the timestamp works if records are only added. An alternative is to use the timestamp of the oldest record returned to get the next set of data.

That way, if a record is deleted, then no record will be skipped.

Using the timestamp of the oldest record returned is also advantageous in query time, since the database doesn't need to count out 100 records to fetch the 101st record, it could simple fetch the first record older than a certain time (which is much faster if your created_at column is indexed - allowing binary search).

like image 156
ronalchn Avatar answered Apr 27 '23 18:04

ronalchn