Find Or Create A Record With FactoryBot
admin = FactoryBot.create(:user, email: 'admin@company.com')admin =
User.find_by(email: 'admin@company.com') ||
FactoryBot.create(:user, email: 'admin@company.com')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
endLast updated