title: NoMethodError with nil object
body: <p>Hi sorry if this is a silly question, realy I'm new in the ROR world and I'm trying to learn. </p>
  
  <p>I'm reading the Agile Web Develpment with Rails eBook and following the book program I got stock with this error: </p>
  
  <blockquote>
    <p>NoMethodError in Line itemsController#create</p>
    
    <p>You have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil.</p>
  </blockquote>
  
  <p>This is the cart Model </p>
  
  <pre><code>class Cart &lt; ActiveRecord::Base
    has_many :line_items, :dependent =&gt; :destroy
  
    def add_product(product_id)
      current_item = line_items.where(:product_id =&gt; product_id).first
      if current_item
        current_item.quantity += 1
      else
        current_item = LineItem.new(:product_id =&gt; product_id)
        line_items &lt;&lt; current_item
      end
      current_item
    end
  end
  </code></pre>
  
  <p>And this is the Line Item controller which method is being called </p>
  
  <pre><code>  def create
      @cart = current_cart
      product = Product.find(params[:product_id])    
      @line_item = @cart.add_product(product.id)
  
      respond_to do |format|
        if @line_item.save
          format.html { redirect_to(@line_item.cart, :notice =&gt; 'Line item was successfully created.') }
          format.xml  { render :xml =&gt; @line_item.cart, :status =&gt; :created, :location =&gt; @line_item }
        else
          format.html { render :action =&gt; "new" }
          format.xml  { render :xml =&gt; @line_item.errors, :status =&gt; :unprocessable_entity }
        end
      end
    end
  </code></pre>
  
  <p>Rails version 3.0.5
  Ruby version 1.8.7</p>
  
  <p>Any suggestion ? can you see what is wrong ? 
  Thanks </p>