In a Rails app, I'm using FactoryGirl to define a general factory plus several more specific traits. The general case and all but one of the traits have a particular association, but I'd like to define a trait where that association is not created/built. I can use an after
callback to set the association's id
to nil
, but this doesn't stop the association record from being created in the first place.
Is there a way in a trait definition to completely disable the creation/building of an association that has been defined for the factory the trait belongs to?
For example:
FactoryGirl.define do
factory :foo do
attribute "value"
association :bar
trait :one do
# This has the bar association
end
trait :two do
association :bar, turn_off_somehow: true
# foos created with trait :two will have bar_id = nil
# and an associated bar will never be created
end
end
end