title: Using factory_girl in Rails with associations that have unique constraints. Getting duplicate errors
body: <p>I'm working with a Rails 2.2 project working to update it. I'm replacing existing fixtures with factories (using factory_girl) and have had some issues. The problem is with models that represent tables with lookup data. When I create a Cart with two products that have the same product type, each created product is re-creating the same product type. This errors from a unique validation on the ProductType model.</p>
  
  <h2>Problem Demonstration</h2>
  
  <p>This is from a unit test where I create a Cart and put it together in pieces. I had to do this to get around the problem. This still demonstrates the problem though. I'll explain.</p>
  
  <pre><code>cart = Factory(:cart)
  cart.cart_items = [Factory(:cart_item, 
                             :cart =&gt; cart, 
                             :product =&gt; Factory(:added_users_product)),
                     Factory(:cart_item, 
                             :cart =&gt; cart, 
                             :product =&gt; Factory(:added_profiles_product))]
  </code></pre>
  
  <p>The two products being added are of the same type and when each product is created it is re-creating the product type and creating duplicates.</p>
  
  <p>The error that gets generated is:
  "ActiveRecord::RecordInvalid: Validation failed: Name has already been taken, Code has already been taken"</p>
  
  <h2>Workaround</h2>
  
  <p>The workaround for this example is to override the product type being used and pass in a specific instance so only one instance is used. The "add_product_type" is fetched early and passed in for each cart item.</p>
  
  <pre><code>cart = Factory(:cart)
  prod_type = Factory(:add_product_type)   #New
  cart.cart_items = [Factory(:cart_item,
                             :cart =&gt; cart,
                             :product =&gt; Factory(:added_users_product,
                                                 :product_type =&gt; prod_type)), #New
                     Factory(:cart_item,
                             :cart =&gt; cart,
                             :product =&gt; Factory(:added_profiles_product,
                                                 :product_type =&gt; prod_type))] #New
  </code></pre>
  
  <h2>Question</h2>
  
  <p>What is the best way to use factory_girl with "pick-list" types of associations? </p>
  
  <p>I'd <em>like</em> for the factory definition to contain everything instead of having to assemble it in the test, although I can live with it.</p>
  
  <h2>Background and Extra Details</h2>
  
  <p>factories/product.rb</p>
  
  <pre><code># Declare ProductTypes
  
  Factory.define :product_type do |t|
    t.name "None"
    t.code "none"
  end
  
  Factory.define :sub_product_type, :parent =&gt; :product_type do |t|
    t.name "Subscription"
    t.code "sub"
  end
  
  Factory.define :add_product_type, :parent =&gt; :product_type do |t|
    t.name "Additions"
    t.code "add"
  end
  
  # Declare Products
  
  Factory.define :product do |p|
    p.association :product_type, :factory =&gt; :add_product_type
    #...
  end
  
  Factory.define :added_profiles_product, :parent =&gt; :product do |p|
    p.association :product_type, :factory =&gt; :add_product_type
    #...
  end
  
  Factory.define :added_users_product, :parent =&gt; :product do |p|
    p.association :product_type, :factory =&gt; :add_product_type
    #...
  end
  </code></pre>
  
  <p>The purpose of ProductType's "code" is so the application can give special meaning to them. The ProductType model looks something like this:</p>
  
  <pre><code>class ProductType &lt; ActiveRecord::Base
    has_many :products
  
    validates_presence_of :name, :code
    validates_uniqueness_of :name, :code
    #...
  end
  </code></pre>
  
  <p>factories/cart.rb</p>
  
  <pre><code># Define Cart Items
  
  Factory.define :cart_item do |i|
    i.association :cart
    i.association :product, :factory =&gt; :test_product
    i.quantity 1
  end
  
  Factory.define :cart_item_sub, :parent =&gt; :cart_item do |i|
    i.association :product, :factory =&gt; :year_sub_product
  end
  
  Factory.define :cart_item_add_profiles, :parent =&gt; :cart_item do |i|
    i.association :product, :factory =&gt; :add_profiles_product
  end
  
  # Define Carts
  
  # Define a basic cart class. No cart_items as it creates dups with lookup types.
  Factory.define :cart do |c|
    c.association :account, :factory =&gt; :trial_account
  end
  
  Factory.define :cart_with_two_different_items, :parent =&gt; :cart do |o|
    o.after_build do |cart|
      cart.cart_items = [Factory(:cart_item, 
                                 :cart =&gt; cart, 
                                 :product =&gt; Factory(:year_sub_product)),
                         Factory(:cart_item, 
                                 :cart =&gt; cart, 
                                 :product =&gt; Factory(:added_profiles_product))]
    end
  end
  </code></pre>
  
  <p>When I try to define the cart with two items of the same product type, I get the same error described above.</p>
  
  <pre><code>Factory.define :cart_with_two_add_items, :parent =&gt; :cart do |o|
    o.after_build do |cart|
      cart.cart_items = [Factory(:cart_item,
                                 :cart =&gt; cart,
                                 :product =&gt; Factory(:added_users_product)),
                         Factory(:cart_item,
                                 :cart =&gt; cart,
                                 :product =&gt; Factory(:added_profiles_product))]
    end
  end
  </code></pre>