> 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/fetching-the-query/model-attributes.md).

# Model extra attributes

Sometimes a query selects fields that are not model columns: aggregate values, computed fields, or columns from joined tables.

By default, Lustra initializes the model columns it knows about. Use `fetch_columns: true` when you also want to keep custom selected fields in the model's `attributes` hash.

```crystal
posts = Post.query
  .select("posts.*", "COUNT(post_tags.id) AS tags_count")
  .left_join(:post_tags) { post_tags.post_id == posts.id }
  .group_by("posts.id")

posts.each(fetch_columns: true) do |post|
  puts post.title
  puts post.attributes["tags_count"]
end
```

`post.title` uses the generated model accessor. `tags_count` is not a model column, so it is read from `attributes`.

You can also use `[]` and `[]?` on the model:

```crystal
post["tags_count"]  # raises if the key is missing
post["tags_count"]? # nil if the key is missing
```

`fetch_columns: true` is available on model-fetching helpers such as:

* `each`
* `map`
* `to_a`
* `first` / `first!`
* `last` / `last!`
* `find_by` / `find_by!`
* `[]`, `[]?`, and range access
* `each_with_cursor`

{% hint style="info" %}
`fetch_columns: true` stores every returned SQL field in `attributes`, so it has extra allocation cost. Leave it disabled unless you need custom selected fields.
{% endhint %}

This pattern is useful for reusable computed fields:

```crystal
posts = Post.query
  .with_count(:tags, alias_name: "tags_count")

posts.each(fetch_columns: true) do |post|
  puts post.title
  puts post["tags_count"]
end
```

If you only need raw rows and not models, use `fetch` instead:

```crystal
Post.query
  .select("posts.id", "COUNT(post_tags.id) AS tags_count")
  .left_join(:post_tags) { post_tags.post_id == posts.id }
  .group_by("posts.id")
  .fetch do |row|
    puts row["tags_count"]
  end
```


---

# 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/fetching-the-query/model-attributes.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.
