<?xml version="1.0" encoding="UTF-8"?>
<post>
  <blog-id type="integer"></blog-id>
  <body>&lt;p&gt;If you are using &lt;a href="http://github.com/thoughtbot/factory_girl/tree/master"&gt;Factory Girl&lt;/a&gt;, then you may be wondering how to define the factories for those &lt;code&gt;has_many :through&lt;/code&gt; associations you have.&lt;/p&gt;

&lt;h2&gt;Models&lt;/h2&gt;

&lt;p&gt;The example models we will use are:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class BeeKeeper &amp;lt; ActiveRecord::Base
  has_many :bees
  has_many :hives, :through =&amp;gt; :bees
end

class Hive &amp;lt; ActiveRecord::Base
  has_many :bees
  has_many :bee_keepers, :through =&amp;gt; :bees
end

class Bee &amp;lt; ActiveRecord::Base
  belongs_to :bee_keeper
  belongs_to :hive
end
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Factories for Testing&lt;/h2&gt;

&lt;p&gt;So, to test these relationships you would define Factories.
Let's start by specifying the belongs_tos&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Factory.define :bee do |b|
  b.bee_keeper {|a| a.association(:bee_keeper)}
  b.hive {|a| a.association(:hive)}
end

Factory.define :bee_keeper do |bk|
end

Factory.define :hive do |h|
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By creating a bee, it should also create a beekeeper and a hive.&lt;/p&gt;

&lt;h2&gt;We're Done!&lt;/h2&gt;

&lt;p&gt;Actually, this is all you need to do for &lt;code&gt;has_many :through&lt;/code&gt; - when you add hives to bee_keepers, they will automatically create the bees!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;context "Bee Keepers" do
  setup do
    @bee_keeper = Factory(:bee_keeper)
    @bee_keeper.hives &amp;lt;&amp;lt; Factory(:hive)
    @bee_keeper.hives &amp;lt;&amp;lt; Factory(:hive)
  end

  should "have 2 hives" do
    assert_equal 2, @bee_keeper.hives.length
  end

  should "have 2 bees" do
    assert_equal 2, @bee_keeper.bees.length
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will create:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 bee keeper&lt;/li&gt;
&lt;li&gt;2 bees&lt;/li&gt;
&lt;li&gt;2 hives&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope this helps some of you.&lt;/p&gt;</body>
  <body-raw>If you are using [Factory Girl](http://github.com/thoughtbot/factory_girl/tree/master), then you may be wondering how to define the factories for those `has_many :through` associations you have.

## Models

The example models we will use are:
	
    class BeeKeeper &lt; ActiveRecord::Base
      has_many :bees
      has_many :hives, :through =&gt; :bees
    end

    class Hive &lt; ActiveRecord::Base
      has_many :bees
      has_many :bee_keepers, :through =&gt; :bees
    end

    class Bee &lt; ActiveRecord::Base
      belongs_to :bee_keeper
      belongs_to :hive
    end

## Factories for Testing

So, to test these relationships you would define Factories.
Let's start by specifying the belongs_tos

    Factory.define :bee do |b|
      b.bee_keeper {|a| a.association(:bee_keeper)}
      b.hive {|a| a.association(:hive)}
    end

    Factory.define :bee_keeper do |bk|
    end
	
    Factory.define :hive do |h|
    end

By creating a bee, it should also create a beekeeper and a hive.

## We're Done!

Actually, this is all you need to do for `has_many :through` - when you add hives to bee_keepers, they will automatically create the bees!

    context "Bee Keepers" do
      setup do
        @bee_keeper = Factory(:bee_keeper)
        @bee_keeper.hives &lt;&lt; Factory(:hive)
        @bee_keeper.hives &lt;&lt; Factory(:hive)
      end
	
      should "have 2 hives" do
        assert_equal 2, @bee_keeper.hives.length
      end

      should "have 2 bees" do
        assert_equal 2, @bee_keeper.bees.length
      end
    end

This will create:

* 1 bee keeper
* 2 bees
* 2 hives

Hope this helps some of you.</body-raw>
  <created-at type="datetime">2009-04-24T07:23:00-07:00</created-at>
  <id type="integer">378</id>
  <status type="integer">1</status>
  <title>Factory Girl and has_many :through</title>
  <updated-at type="datetime">2009-04-24T07:32:35-07:00</updated-at>
</post>
