title: How to set up factory in FactoryBot with has_many association
body: <p>Can someone tell me if I'm just going about the setup the wrong way?</p>
  
  <p>I have the following models that have has_many.through associations:</p>
  
  <pre><code>class Listing &lt; ActiveRecord::Base
    attr_accessible ... 
  
    has_many :listing_features
    has_many :features, :through =&gt; :listing_features
  
    validates_presence_of ...
    ...  
  end
  
  
  class Feature &lt; ActiveRecord::Base
    attr_accessible ...
  
    validates_presence_of ...
    validates_uniqueness_of ...
  
    has_many :listing_features
    has_many :listings, :through =&gt; :listing_features
  end
  
  
  class ListingFeature &lt; ActiveRecord::Base
    attr_accessible :feature_id, :listing_id
  
    belongs_to :feature  
    belongs_to :listing
  end
  </code></pre>
  
  <p>I'm using Rails 3.1.rc4, FactoryGirl 2.0.2, factory_girl_rails 1.1.0, and rspec.  Here is my basic rspec rspec sanity check for the <code>:listing</code> factory:</p>
  
  <pre><code>it "creates a valid listing from factory" do
    Factory(:listing).should be_valid
  end
  </code></pre>
  
  <p>Here is Factory(:listing)</p>
  
  <pre><code>FactoryGirl.define do
    factory :listing do
      headline    'headline'
      home_desc   'this is the home description'
      association :user, :factory =&gt; :user
      association :layout, :factory =&gt; :layout
      association :features, :factory =&gt; :feature
    end
  end
  </code></pre>
  
  <p>The <code>:listing_feature</code> and <code>:feature</code> factories are similarly setup.<br>
  If the <code>association :features</code> line is commented out, then all my tests pass.<br>
  When it is </p>
  
  <pre><code>association :features, :factory =&gt; :feature
  </code></pre>
  
  <p>the error message is 
  <code>undefined method 'each' for #&lt;Feature&gt;</code>  which I thought made sense to me because because <code>listing.features</code> returns an array.  So I changed it to </p>
  
  <pre><code>association :features, [:factory =&gt; :feature]
  </code></pre>
  
  <p>and the error I get now is <code>ArgumentError: Not registered: features</code>  Is it just not sensible to be generating factory objects this way, or what am I missing?  Thanks very much for any and all input!</p>