thoughtbot/factory_girl — Issue #252: Create or build factory for an association
Author: Axsuul
Created: 2011-12-13 01:56:59+00:00
State: open

Some of my factories are unique so when I associate those unique factories with other factories, I want to either create the associated factory if it doesn't exist, or build it.

```ruby
factory :store, :class => Store do
sequence(:name) { |n| "Store ##{n}" }

trait :starbucks do
name "Starbucks"
end

factory :store_starbucks, :traits => [:starbucks]
end

factory :product, :class => Product do
sequence(:name) { |n| "Some random product ##{n}" }

trait :coffee do
name "Coffee"
store { Factory.build_or_create(:store_starbucks) } # pseudo code
end

factory :product_coffee, :traits => [:coffee]
end
```

Is there something like this FactoryGirl or is there a better way to do this?