title: Rspec, Factory Girl build with has_many
body: <p>I have a model with one to many relationship, they are Poll and Choice.</p>
  
  <p>How do I test them correctly, because below code causing <code>ActiveRecord::RecordInvalid:</code>. I want to create the parent (<code>Poll</code>) and its children (<code>Choices</code>) on the go, instead of create the parent (<code>Poll</code>) first then save the children (<code>Choices</code>) after that. </p>
  
  <p>Here are the codes:</p>
  
  <p>First, the error I got for all test cases:</p>
  
  <pre><code>Failure/Error: @poll = FactoryGirl.build(:poll_with_choices, user: @user)
   ActiveRecord::RecordInvalid:
     Validation failed: Choices choices required at least 2
  </code></pre>
  
  <p><strong>Poll model:</strong></p>
  
  <pre><code>class Poll &lt; ActiveRecord::Base
    belongs_to :user
    has_many :choices
    accepts_nested_attributes_for :choices
  
    validates :title, presence: true
    validates_each :choices do |record, attr, value| 
      record.errors.add attr, "choices required at least 2" if record.choices.length &lt; 2
    end
  end
  </code></pre>
  
  <p><strong>Poll factory:</strong></p>
  
  <pre><code>FactoryGirl.define do
    factory :poll do
      title { FFaker::Lorem.phrase }
      description { FFaker::Lorem.sentences }
      user
  
      factory :poll_with_choices do  
        transient do 
          choices_count 3
        end
  
        after(:build) do |poll, evaluator|
          build_list(:choice, evaluator.choices_count)
        end
      end
    end
  end
  </code></pre>
  
  <p><strong>Choice factory:</strong></p>
  
  <pre><code>FactoryGirl.define do
    factory :choice do
      label { FFaker::Name.name }
      votes 0
      poll
    end
  end
  </code></pre>
  
  <p><strong>Poll spec</strong></p>
  
  <pre><code>require 'rails_helper'
  
  RSpec.describe Poll, type: :model do
    before do 
      @user = FactoryGirl.create(:user)
      @poll = FactoryGirl.build(:poll_with_choices, user: @user) 
    end
  
    subject { @poll }
  
    it { should respond_to(:title) }
    it { should respond_to(:description) }
  
    it { should validate_presence_of(:title) }
  
    it { should belong_to(:user) }
    it { should have_many(:choices) }
    it { should accept_nested_attributes_for(:choices) }
  
    describe "#save" do 
      before do 
        @user = FactoryGirl.create(:user)
      end
  
      it "success" do 
        poll = FactoryGirl.build(:poll_with_choices, user: @user)
        expect(poll.save).to eql true
      end
  
      it "fail" do 
        poll = FactoryGirl.build(:poll, user: @user)
        poll.choices = FactoryGirl.build_list(:choice, 1)
        expect(poll.save).to eql false
      end
    end
  end
  </code></pre>
  
  <p>As reference for <code>FactoryGirl.create</code>, <strong>not</strong> <code>FactoryGirl.build</code>: <a href="http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md#Associations" rel="nofollow">http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md#Associations</a></p>
  
  <p>Thanks, in advance.</p>