id |
name |
1 |
Jane |
2 |
Max |
3 |
John |
4 |
Scott |
5 |
Mark |
result = User.after(3)
=>
[
{ id: 4, name: "Scott" },
{ id: 5, name: "Mark" }
]
result.has_more?
=> false
result.total_size
=> 2
result.total_count
=> 6
result = User.before(3)
=>
[
{ id: 2, name: "Max" },
{ id: 1, name: "Jane" }
]
result.has_more?
=> false
result.total_size
=> 2
result.total_count
=> 6
result = User.after(1).limit(1)
=>
[
{ id: 2, name: "Max" }
]
result.has_more?
=> true
result.total_size
=> 5
result.total_count
=> 6
result = User.after(1).before(3)
=>
[
{ id: 2, name: "Max" }
]
result.has_more?
=> false
result.total_size
=> 1
result.total_count
=> 6
That's a bad implementation of cursor based pagination. You break the sorting. What if my collection sorted by name? But I want to get new collection skipping objects up to some id, but remain the same sorting options. I.e we have this:
User.order(:name)
will return me full collection:Of course I want to paginate, so I limit my collection with
User.order(:name).limit(2)
(in real life it could be not 2 but 20):And what if I want the second page? But I don't want to use
offset
, if you've got here to see "cursor pagination" instead of a simple pagination, then you probably know why you need this. So what?I use
User.order(:name).after(3).limit(2)
and should get this:But your solution will reorder my collection by id, returning some nonsense