struct

PgORM::PaginatedResult(T)

Inherits Struct < Value < Object

Result wrapper for offset-based pagination containing records and metadata.

Records are loaded lazily - the database query doesn't execute until you access the records. This allows you to pass pagination results around without loading data unnecessarily.

Metadata Available

  • total: Total number of records matching the query
  • limit: Number of records per page
  • offset: Number of records skipped
  • page: Current page number (1-indexed)
  • total_pages: Total number of pages
  • has_next?: Whether there's a next page
  • has_prev?: Whether there's a previous page
  • next_page: Next page number (or nil)
  • prev_page: Previous page number (or nil)
  • from: Starting record number (1-indexed)
  • to: Ending record number (1-indexed)

Example

result = User.where(active: true).paginate(page: 2, limit: 20)

result.total       # => 150
result.page        # => 2
result.total_pages # => 8
result.has_next?   # => true
result.from        # => 21
result.to          # => 40

# Records loaded only when accessed
result.records.each do |user|
  puts user.name
end

# Or iterate directly
result.each do |user|
  puts user.name
end

Constructors

new(query : Collection(T) | Relation(T), total : Int64, limit : Int32, offset : Int32)
Source

Instance methods

each

Iterate over records without loading all into memory at once This allows streaming/processing records one at a time

Source
from

Starting record number (1-indexed)

Source
has_next?

Whether there is a next page

Source
has_prev?

Whether there is a previous page

Source
limit
Source
next_page

Next page number (nil if no next page)

Source
offset
Source
page
Source
prev_page

Previous page number (nil if no previous page)

Source
records

Lazily load records only when accessed Records are cached after first access to avoid multiple DB queries

Source
to

Ending record number (1-indexed)

Source
to_json(json : JSON::Builder)

Convert to JSON with pagination metadata Note: This will load all records into memory for serialization

Source
total
Source
total_pages

Total number of pages

Source