Rails 4 pagination with dynamic data
Asked Answered
D

2

6

Hi I have a rails4 app using pagination with the will_paginate gem.

Is there a way to handle pagination with objects created very often? If you call the first page and right after that the page 2, considering new objects are created in the meantime, it is going to mess data (the offset in the sql request is for example going to call almost the same data as the first page).

Is there a way to use pagination with rapidly changing dynamic data?

Thx for your suggestions

Divinadivination answered 31/5, 2014 at 19:18 Comment(2)
How are you ordering the data? If the amount of total records is really volatile you can opt to not show the final page's number but an "end" indication. The offset shouldn't mess with anything if you're ordering by a create_at timestamp.Euhemerism
The pattern you're after is "cursor" pagination, and it isn't a feature of will_paginate. However, you could implement it yourself fairly easily - add the current date as a parameter to each pagination link, and then filter your ActiveRecord relation to only include records created before that date, if the parameter is present. See sitepoint.com/paginating-real-time-data-cursor-based-pagination for a good description.Isologous
T
0

Also, you could use the cursor gem

Tailpiece answered 3/1, 2017 at 12:37 Comment(0)
W
0

This is a good tutorial showing how to do cursor based pagination without a gem. https://www.ananunesdasilva.com/posts/pagination-in-rails-from-scratch-no-gems I've also saved it in archive.org/wayback machine in case the author doesn't maintain the website in a few years.

Wend answered 11/2, 2022 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.