> 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/model/associations.md).

# Associations

Lustra supports the common Active Record style associations:

| Association              | Meaning                                                                                  |
| ------------------------ | ---------------------------------------------------------------------------------------- |
| `belongs_to`             | The current model stores a foreign key pointing to another model.                        |
| `has_many`               | The related model stores a foreign key pointing back to the current model.               |
| `has_many through`       | Models are connected through a join model/table.                                         |
| `has_one`                | The related model stores a foreign key, but only zero or one related record is expected. |
| Polymorphic associations | One child model can belong to one of several parent model types.                         |

Associations generate query helpers, assignment helpers, and eager-loading helpers such as `with_books`.

By default, Lustra follows naming conventions. You can override foreign keys and primary keys when your schema uses different names.

## Association Collections

Collection associations return query collections, not plain arrays. This means you can refine an association query before executing it.

```crystal
author = Author.find!(1)

books = author
  .books
  .where { published_at != nil }
  .order_by(published_at: :desc)
  .limit(20)
```

The query is executed by terminal methods such as `each`, `to_a`, `first`, `count`, or `empty?`.

```crystal
books.each do |book|
  puts book.title
end
```

Association collections can also be combined with scopes and eager-loading helpers:

```crystal
books = author
  .books
  .with_reviews
  .with_count(:reviews, alias_name: "reviews_count")
  .order_by("books.id", :asc)
```

The same pattern works from `has_many through` associations:

```crystal
orders = book
  .orders
  .where(status: "paid")
  .order_by(date_submitted: :desc)
```

See [Polymorphic Associations](/lustra-docs/model/associations/polymorphic-associations.md) when a child model can belong to more than one parent model type.


---

# 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/model/associations.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.
