> 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/specify-new-attributes-for-find-or-create-by.md).

# Specify New Attributes For #find\_or\_create\_by

The ActiveRecord [`#find_or_create_by`](https://apidock.com/rails/v4.0.2/ActiveRecord/Relation/find_or_create_by) method is a handy way to get an object that represents a record. It will attempt to look up that record, usually based on a unique value or set of values. If it can find one, then that's the record you get. If nothing is found, then it will create a new record.

New records tend to need more data than just the unique lookup attribute. There are a couple ways these other attributes can be specified.

The first is by giving `#find_or_create_by` a block.

```ruby
User.find_or_create_by(email: "some@email.com") do |new_user|
  new_user.admin = false
  new_user.password = params[:password]
  new_user.password_confirm = params[:password_confirm]
end
```

Another approach is to precede the `#find_or_create_by` call with a [`#create_with`](https://apidock.com/rails/ActiveRecord/QueryMethods/create_with) call.

```ruby
User.create_with(
  admin: false,
  password: params[:password],
  password_confirm: params[:password_confirm]
).find_or_create_by(email: "some@email.com")
```

In both cases, the extra attributes will not be applied to the `User` record in the case of a *find*; they are only used in the case of a *create*.


---

# 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/specify-new-attributes-for-find-or-create-by.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.
