# Fixing FactoryBot Validation Name Has Already Been Taken Controller Test or Spec Errors

DevFeed: [Fixing FactoryBot Validation Name Has Already Been Taken Controller Test or Spec Errors](<https://devfeed.tech/articles/fixing-factorybot-validation-name-has-already-been-taken-controller-test-or-spec-errors-28117.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/2022/06/10/fixing-factorybot-validation-name-has-already-been-taken-controller-test-or-spec-errors.html>)

Author: Fuzzygroup

Published: 2022-06-10T12:41:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [Development](<https://devfeed.tech/topics/development.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [bug](<https://devfeed.tech/tags/bug.md>), [controllers](<https://devfeed.tech/tags/controllers.md>), [development](<https://devfeed.tech/tags/development.md>), [factorybot](<https://devfeed.tech/tags/factorybot.md>), [rails](<https://devfeed.tech/tags/rails.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

This troubleshooting article examines recurring ActiveRecord "Name has already been taken" failures in FactoryBot-backed Rails controller tests. It describes investigating associations, test data, database cleaning, and model accessors while tracing the source of the validation behavior.

## Source excerpt

So, last night, whilst pulling vampire hours on writing tests - always, always, always a bad idea - I encountered multiple variants on this bug: ❯ bundle exec rails test test/controllers/links_controller_test.rb:44 Running 7 tests in a single process (parallelization threshold is 50) Run options: --seed 21798 # Running: E Error: LinksControllerTest#test_should_get_edit: ActiveRecord::RecordInvalid: Validation failed: Name has already been taken test/controllers/links_controller_test.rb:47:in `block in <class:LinksControllerTest>' This was particularly vexing because while the object in question did have a name attribute, it did not have name validations: ######################################################################### # # validations # ######################################################################### validates_presence_of :project_id validates_presence_of :team_id validates_presence_of :account_id validates_presence_of :link_type_id There are a number of people on the internet that have this issue including this stack overflow post. What I noticed as I researched this is that I only saw it in controller tests. Also my model tests continued to function correctly without any validation issues. I had initially discovered this while working on my RodAuth testing deep dive and back burnered it until I got past that particularly nasty kettle of fix. I was working with two objects, link and project and project only through an association. So my Factory for link looked like this: FactoryBot.define do factory :link do url { "https://www.example.com/?time=#{Time.now.to_i}" } name { "Cartazzi App on development #{Time.now.to_i}" } active { true } association :account association :team association :project association :link_type end end You will notice the insane use of Time.now.to_i to try and generate more distinct data. This was an attempt to see if maybe some kind of hidden validation was present (it is ruby and everything is dynamic; who knows where a monk