title: Validating That A has_many Association Has At Least One Model When Using FactoryGirl
body: <p>Putting aside arguments on whether or not you should test existence of a model's associations, I have a model called <code>Order</code> and I am validating that it has at least one <code>item</code> in its <code>has_many</code> association using:</p>
  
  <pre><code>class Order &lt; ActiveRecord::Base
  
    has_many :items
  
    validates :items, presence: true
  
  end
  </code></pre>
  
  <p>I have set FactoryGirl to lint my factories (checking for validity). So my order factory is not valid unless I create an item for its <code>has_many</code> collection.</p>
  
  <p>My <code>orders</code> factory looks like this:</p>
  
  <pre><code>FactoryGirl.define do
  
    factory :order do
  
      ignore do
        items_count 1
      end
  
      after(:build) do |order, evaluator|
        create_list(:item, evaluator.items_count, order: order)
      end
  
    end
  end
  </code></pre>
  
  <p>According to Factory Girl's <em>Getting Started</em>:</p>
  
  <pre><code>FactoryGirl.lint builds each factory and subsequently calls #valid? on it
  </code></pre>
  
  <p>However when I run my specs, Factory Girl throws an <code>FactoryGirl::InvalidFactoryError</code> because the order factory is invalid.</p>
  
  <p><strong>Workaround</strong></p>
  
  <pre><code>after(:build) do |order, evaluator|
     evaluator.items_count.times do
       order.items &lt;&lt; FactoryGirl.create(:item)
     end
     #create_list(:item, evaluator.items_count, order: order)
   end
  </code></pre>