title: RSpec - Testing that an active record association is set
body: <p>I'm having trouble testing the following. When creating an object a status of "Pending" is assigned. The status is actually an active record association. So it looks like this</p>
  
  <pre><code>def object
    belongs_to :object_status
  end
  
  def object_status
    has_many :objects
  
    def self.pending
      find_by_name('pending')
    end
  end
  </code></pre>
  
  <p>Then in the controller create it does this</p>
  
  <pre><code>@object = Object.new(params[:object])
  @object.object_status = ObjectStatus.pending
  @object.save
  </code></pre>
  
  <p>In RSpec I want to test to make sure that the object has a status of pending so I'm doing this in my objects_controller_spec:</p>
  
  <pre><code>post :create, :object =&gt; @attr # I'm using pre-defined attributes
  p = Object.last
  p.object_status_id.should eq(Factory(:object_status_pending).id)
  </code></pre>
  
  <p>The RSpec test fails with the following (16 is the id of the Factory object):
  expected 16 
  got nil
  For some reason it isn't retrieving the value for the object_status_id. If I run through the code in my dev server the object in the database has the object_status_id set properly, so I know the code passes the test. I have multiple instances of this that I can't figure out.</p>
  
  <p>Thanks</p>