> 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/find-or-create-a-record-with-factory-bot.md).

# Find Or Create A Record With FactoryBot

I have a bunch of tests throughout my test suite that rely on a particular kind of unique record. Let's say it is a special admin user.

```ruby
admin = FactoryBot.create(:user, email: 'admin@company.com')
```

If this user has already been created then trying to re-create it with [FactoryBot](https://github.com/thoughtbot/factory_bot) will result in a unique email validation error.

Another way to approach this would be to either find or create the admin user. In some standard Rails code that might look like this:

```ruby
admin =
  User.find_by(email: 'admin@company.com') ||
  FactoryBot.create(:user, email: 'admin@company.com')
```

There is some repetitiveness to this that I'd like to avoid. FactoryBot doesn't have an equivalent to ActiveRecord's `find_and_create_by`, but we can work around this.

We can add an `initialize_with` directive to the `User` factory.

```ruby
FactoryBot.define do
  factory :user do
    sequence(:email) { |n| 'user#{n}@example.com' }

    # a bunch of other attributes

    initialize_with { User.find_or_create_by(email: email) }
  end
end
```

With this in place, we can call `FactoryBot.create` with the already existing *admin* user and it will look up the record instead of raising a validation error.

[source](https://stackoverflow.com/a/11799674/535590)


---

# 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/find-or-create-a-record-with-factory-bot.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.
