title: `find_by_*_id` does not work as expected
body: <p>I'm following the book <a href="http://pragprog.com/book/rails4/agile-web-development-with-rails" rel="nofollow">Agile Web Development with Rails (4th edition)</a> and I'm having a trouble with a failing test case.</p>
  
  <p>I wrote a test that checks that when you add a duplicate item into a cart, the number of items does not change, but the quantity changes</p>
  
  <pre><code>  test 'adding duplicate products to cart' do
      cart = Cart.new
  
      ruby_book = products(:ruby)
      cart.add_product ruby_book.id
  
      assert cart.line_items.length == 1
      assert cart.total_price == ruby_book.price
  
      p '-------start--------'
      p 'outside add_product'
      p "line_items: #{cart.line_items}"
      cart.add_product ruby_book.id
  
      assert cart.line_items.length == 1  # TEST FAILS HERE with length == 2
      assert cart.total_price == ruby_book.price * 2 
    end 
  </code></pre>
  
  <p>It basically tests the <code>add_product</code> method, which is:</p>
  
  <pre><code>class Cart &lt; ActiveRecord::Base
    has_many :line_items, dependent: :destroy
  
    def add_product(product_id)
      p 'inside add_product'
      p "line_items: #{line_items}"
      p "product_id: #{product_id}"
      current_item = line_items.find_by_product_id(product_id)
      p "current_item: #{current_item}"
      p '---------end---------'
      if current_item
        current_item.quantity += 1
        current_item.price += current_item.product.price
      else
        current_item = line_items.build(product_id: product_id)
        current_item.price = current_item.product.price
      end 
      current_item
    end 
  
    def total_price
      line_items.to_a.sum { |item| item.price }
    end 
  end
  </code></pre>
  
  <p>To debug the test I added some print calls (as you see). The output is:</p>
  
  <pre><code>"-------start--------"
  "outside add_product"
  "line_items: [#&lt;LineItem id: nil, product_id: 207281424, cart_id: nil, created_at: nil, updated_at: nil, quantity: 1, price: #&lt;BigDecimal:7fa491744fe0,'0.4999E2',18(45)&gt;&gt;]"
  "inside add_product"
  "line_items: [#&lt;LineItem id: nil, product_id: 207281424, cart_id: nil, created_at: nil, updated_at: nil, quantity: 1, price: #&lt;BigDecimal:7fa491744fe0,'0.4999E2',18(45)&gt;&gt;]"
  "product_id: 207281424"
  "current_item: "
  "---------end---------"
  </code></pre>
  
  <p>I can see that <code>line_items</code> has an item with <code>product_id: 207281424</code> but for some reason the call <code>line_items.find_by_product_id(product_id)</code> does not return the item, but returns <code>nil</code>. Why?</p>