PgORM::CursorPaginatedResult(T)
Inherits Struct < Value < Object
Result wrapper for cursor-based pagination.
Cursor pagination is more efficient than offset pagination for large datasets because it doesn't require counting all records or skipping rows. Instead, it uses the primary key (or another column) as a cursor to fetch the next or previous page.
Advantages over Offset Pagination
- Performance: No OFFSET clause, which gets slower with large offsets
- Consistency: New records don't shift pages during pagination
- Scalability: Works well with millions of records
Limitations
- Can't jump to arbitrary pages (only next/previous)
- No total count or page numbers
- Requires a sortable cursor column (usually primary key)
Example
# First page
result = Article.order(:id).paginate_cursor(limit: 20)
result.records.each { |article| puts article.title }
# Next page (using cursor from previous result)
if result.has_next?
next_result = Article.order(:id).paginate_cursor(
after: result.next_cursor,
limit: 20
)
end
# Previous page
if result.has_prev?
prev_result = Article.order(:id).paginate_cursor(
before: result.prev_cursor,
limit: 20
)
end
Constructors
new(records_array : Array(T), limit : Int32, next_cursor : String | Nil = nil, prev_cursor : String | Nil = nil)
SourceInstance methods
limit
Sourcenext_cursor
Sourceprev_cursor
Source