Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the same value for two attributes in FactoryGirl

I'm setting up an Artist model that has nameand birth_name attributes. In some cases, an artist's stage name is synonomous with their real name, vice-versa. What I'd like to do in my factory is use the birth_name (created using Faker) as the name attribute. I tried simply referencing as so:

FactoryGirl.define do
  factory :artist do
    name { birth_name }
    birth_name { Faker::Name.name }
  end
end

but get this error:

ArgumentError: Factory not registered: birth_name

What's the best way to get this to work?

like image 398
Carl Edwards Avatar asked Jul 02 '16 15:07

Carl Edwards


1 Answers

FactoryBot.define do
  factory :artist do
    birth_name { Faker::Name.name }
    name { birth_name }
  end
end

Reference:
FactoryBot - Dependent Attributes

like image 164
fabriciofreitag Avatar answered Nov 17 '22 11:11

fabriciofreitag