> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/creating-records-of-has-one-associations.md).

# Creating Records of Has\_One Associations

When working with a model, say a User, that has a `has_many` association with another model, say a Post, you can create a new post for a user like so:

```ruby
u1 = User.first
=> #<User:0x...>
u1.posts.create(title: "Some Title", content: "...")
=> #<Post:0x...>
```

What about with a `has_one` association? Consider a Customer that has a `has_one` association with an Account. Rails provides this method for you:

```ruby
c1.create_account(account_number: 123, ...)
=> #<Account:0x...>
```

Rails also gives you a similar `build` method:

```ruby
c1.build_account(account_number: 123, ...)
=> #<Account:0x...>
```


---

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

```
GET https://ploegert.gitbook.io/til/programmy/rails/creating-records-of-has-one-associations.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.
