# has\_many

Has many represents one of the counter part of [belongs to](/project/model/associations/belongs_to.md) relation. It assumes the current model is referenced by a collection of another model.

Let's see the example used in the chapter [`belongs_to`](/project/model/associations/belongs_to.md):

```sql
CREATE TABLE categories (
    id bigserial NOT NULL PRIMARY KEY,
    name text NOT NULL
)

CREATE TABLE posts (
    id bigserial NOT NULL PRIMARY KEY,
    name text NOT NULL,
    content text,
    category_id bigint NOT NULL
)
```

```ruby
class Post
  include Clear::Model

  primary_key

  column name : String
  column content : String?

  belongs_to category : Category
end

class Category
  include Clear::Model

  primary_key

  column name : String

  has_many posts : Post
end
```

Here, we said a category has many posts. The posts can be accessed through the method `posts` which return a `Collection`:

```ruby
c = Category.query.find!{name == "Technology"} # Retrieve the category named Technology

c.posts.each do |post|
    puts "Post name: #{post.name}"
end
```

Note: The relation can be refined after fetching:

```ruby
# Fetch only the posts which starts by a digit:
c.posts.where{name =~ /^[0-9]/i}.each do |post|
    puts "Post name: #{post.name}"
end
```

## Customizing the relation

Clear uses naming convention to infer the name of the foreign key. You may want to override this behavior by adding some parameters:

```ruby
has_many relation_name : RelationType,
    foreign_key: "column_name", own_key: "column_name", no_cache: true|false
```

|    Argument   | Description                                                         |         Default value        |
| :-----------: | ------------------------------------------------------------------- | :--------------------------: |
| `foreign_key` | The foreign key which is inside the relative model                  | `[underscore_model_name]_id` |
|   `own_key`   | The key against what the relation is tested, primary key by default |     `self.class.__pkey__`    |
|   `no_cache`  | Never cache the relation (note: planned feature)                    |            `false`           |

## Adding to relation

An object can be added into the relation collection using `<<` operator:

```ruby
c.posts << Post.new({name: "A good post"})
```

In this case, the post is saved during the add operation.


---

# Agent Instructions: 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:

```
GET https://clear.gitbook.io/project/model/associations/has_many.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
