I have a User model that is only valid when there is at least one Address:
class User
has_many :addresses
validates :addresses, length: { miniumum: 1}
end
class Address
belongs_to :user
end
I tried defining my FactoryBot factory like this:
FactoryBot.define do
factory :user do
association :address
name 'test'
end
end
When creating a user with create(:user)
, there's an error that user could not be saved due to missing address. It seems the association is only created after the user is created (which obviously creates a validation error). What's the correct way to build my factory?
Thanks