title: Testing before_save with RSpec and Factory Girl
body: <p>I'm using a before_save call to update all the records that belong to a particular master.  Both have a field called item_id:</p>
  
  <pre><code>class Master &lt; ActiveRecord::Base
    has_many :records
    before_save :update_records
    def update_records
      self.records.find_each do |r|
        r.item_id = self.item_id
        r.save
      end
    end
  end
  
  class Record &lt; ActiveRecord::Base
  end
  </code></pre>
  
  <p>This works as intended (leaving aside for the minute whether it's a good idea to do something like this).  However I can't get an RSpec test that passes.  This is what I tried:</p>
  
  <pre><code>describe Master do
    describe 'before_save' do
      it 'updates records belonging to it' do
        master = FactoryGirl.create(:master, item_id: 1)
        record = FactoryGirl.create(:record, master_id: master.id, item_id: 1)
        master.item_id = 2
        master.save
        record.item_id.should == 2
      end
    end
  end
  </code></pre>
  
  <p>The reason is interesting.  The model does its work on a second instance of a Record model which gets saved successfully, but is not returned to the spec:</p>
  
  <pre><code>"MASTER - CREATED IN SPEC"
  #&lt;Master:0x007fd0ed55ec20&gt; {
      :id =&gt; 26,
      :item_id =&gt; 1,
  }
  "RECORD - CREATED IN SPEC"
  #&lt;Record:0x007fd0ea157e40&gt; {
      :id =&gt; 16,
      :item_id =&gt; 1,
      :master_id =&gt; 26,
  }
  "RECORD - AFTER SAVE IN MODEL"
  #&lt;Record:0x007fd0ed2f9a70&gt; {
      :id =&gt; 16,
      :item_id =&gt; 2,
      :master_id =&gt; 26,
  }
  "MASTER - UPDATED IN SPEC"
  #&lt;Master:0x007fd0ed55ec20&gt; {
      :id =&gt; 26,
      :item_id =&gt; 2,
  }
  "RECORD - FINAL IN SPEC"
  #&lt;Record:0x007fd0ea157e40&gt; {
      :id =&gt; 16,
      :item_id =&gt; 1,
      :master_id =&gt; 26,
  }
  </code></pre>
  
  <p>Do you know:</p>
  
  <ul>
  <li>why this is happening?</li>
  <li>How I can get this spec to pass?</li>
  </ul>