> For the complete documentation index, see [llms.txt](https://crystal-garage.gitbook.io/lustra-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://crystal-garage.gitbook.io/lustra-docs/querying/the-collection-object/filter-the-query-1/pagination.md).

# Pagination

Lustra includes `Lustra::SQL::Query::WithPagination`, which adds pagination helpers to select builders and model collections.

Use `paginate` to fetch one page of records:

```crystal
per_page = 20
page = 3

posts = Post
  .query
  .where(published: true)
  .order_by(created_at: :desc)
  .order_by(id: :asc)
  .paginate(page: page, per_page: per_page)
```

This fetches page 3 with 20 records per page.

Always use a deterministic order for paginated queries. If the main ordering column can have duplicate values, add a stable tie-breaker such as the primary key.

```crystal
Post.query
  .order_by(created_at: :desc)
  .order_by(id: :asc)
```

## Total Count

`paginate` calculates the total count before applying `limit` and `offset`. After calling it, the collection exposes pagination metadata:

```crystal
base = Post
  .query
  .where(published: true)

posts = base
  .order_by(created_at: :desc)
  .order_by(id: :asc)
  .paginate(page: page, per_page: per_page)

posts.total_entries # total matching records
posts.total_pages
posts.current_page
posts.per_page
posts.previous_page
posts.next_page
posts.first_page?
posts.last_page?
posts.out_of_bounds?
```

Because collections are mutable, `paginate` changes the collection by clearing existing `limit` and `offset`, counting the records, and then applying the page limit and offset. Use `dup` when you want to preserve a reusable base query:

```crystal
base = Post.query.where(published: true)

posts = base
  .dup
  .order_by(created_at: :desc)
  .order_by(id: :asc)
  .paginate(page: page, per_page: per_page)
```

## Page Bounds

Applications can use `out_of_bounds?` to detect a page past the end.

```crystal
raise "page out of range" if posts.out_of_bounds?
```

Use the same base query for pagination, so the total and the records are calculated from the same filters.

## Association Pagination

Association collections can be paginated the same way:

```crystal
posts = user
  .posts
  .where(published: true)
  .order_by(created_at: :desc)
  .order_by(id: :asc)
  .paginate(page: page, per_page: per_page)
```

## Manual `limit` and `offset`

You can still use `limit` and `offset` directly when you do not need pagination metadata:

```crystal
offset = (page - 1) * per_page

posts = Post
  .query
  .order_by(created_at: :desc)
  .order_by(id: :asc)
  .limit(per_page)
  .offset(offset)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://crystal-garage.gitbook.io/lustra-docs/querying/the-collection-object/filter-the-query-1/pagination.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
