title: Test::Unit and .first method
body: <p>I have a test:</p>
  
  <pre><code> def setup
      @cart = FactoryGirl.create(:cart_with_1_row)
    end
  
    test "should get show" do
      sign_in(FactoryGirl.create(:user, admin: true))
      FactoryGirl.create(:product_request)
      session[:cart_id] = @cart.id
      get :show, id: @cart.id
      assert_response :success
      assert_not_nil assigns(:product_requests)
    end
  </code></pre>
  
  <p>and a controller:</p>
  
  <pre><code> def show
      cart = current_cart
      @product_requests = ProductRequest.find_all_by_cart_id(current_cart)
      cart.destroy
    end
  </code></pre>
  
  <p>And this fails unless I remove the following from my views:</p>
  
  <pre><code>&lt;h6&gt;&lt;%= @product_requests.first.user.first_name %&gt; &lt;%= @product_requests.first.user.last_name %&gt;&lt;/h6&gt;
  &lt;h6&gt;&lt;%= @product_requests.first.street %&gt;&lt;/h6&gt;
  &lt;h6&gt;&lt;%= @product_requests.first.city %&gt;, &lt;%= @product_requests.first.state %&gt; &lt;%= @product_requests.first.zip %&gt;&lt;/h6&gt;
  </code></pre>
  
  <p>The error is:</p>
  
  <pre><code>  1) Error:
  test_should_get_show(CartsControllerTest):
  ActionView::Template::Error: undefined method `user' for nil:NilClass
  </code></pre>
  
  <p>Which means calling user on .first is creating it? The page works fine in the development and production environment.</p>
  
  <p>As a sanity check I put <code>&lt;%= product_request.user.first_name %&gt;</code> inside my each method on the ivar, and that worked. Am I interpreting this correctly that .first is the problem?</p>
  
  <p>The view in question looks something like this:</p>
  
  <pre><code>&lt;h6&gt;&lt;%= @product_requests.first.user.first_name %&gt; &lt;%= @product_requests.first.user.last_name %&gt;&lt;/h6&gt;
      &lt;h6&gt;&lt;%= @product_requests.first.street %&gt;&lt;/h6&gt;
      &lt;h6&gt;&lt;%= @product_requests.first.city %&gt;, &lt;%= @product_requests.first.state %&gt; &lt;%= @product_requests.first.zip %&gt;&lt;/h6
  
  &lt;% @product_requests.each do |product_request| %&gt;
  &lt;%= product_request.user.first_name %&gt;
  &lt;% end %&gt;
  </code></pre>