Using the same value for two attributes in FactoryGirl
Asked Answered
G

1

10

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?

Glaucous answered 2/7, 2016 at 15:45 Comment(0)
K
21
FactoryBot.define do
  factory :artist do
    birth_name { Faker::Name.name }
    name { birth_name }
  end
end

Reference:
FactoryBot - Dependent Attributes

Katydid answered 2/7, 2016 at 15:59 Comment(3)
That will throw error too. Because that's for actual attributes, not virtual.Gomorrah
Actually this does work for newer versions of FactoryGirlJuetta
This works in factory_bot 4.11.0. one interesting thing is if you have mistakenly provide the same attribute name both inside and outside, entire thing will crash with a segfault: name { name }Demobilize

© 2022 - 2024 — McMap. All rights reserved.