title: Rails - getting values from a model into the application layout
body: <p>I'm wanting to get the 'current_item.quantity' from a model for use in a view - i.e. I want to be able to put '(x) items currently in your cart' in the application layout view. How would I go about doing this? Have tried every combination of '@total_current_items' etc. that I can think of. Thanks!!</p>
  
  <p>Here's the model code if it helps:</p>
  
  <pre><code>class Cart &lt; ActiveRecord::Base
  
  has_many :line_items, dependent: :destroy
  
  def add_product(product_id)
    current_item = line_items.find_by_product_id(product_id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(:product_id =&gt; product_id)
      current_item.price = current_item.product.price
    end
    current_item
  end
  
  def total_price
    line_items.to_a.sum { |item| item.total_price }
  end
  
  def decrease(line_item_id)
    current_item = line_items.find(line_item_id)
    if current_item.quantity &gt; 1
      current_item.quantity -= 1
    else
      current_item.destroy
    end
    current_item
  end
  
  def increase(line_item_id)
    current_item = line_items.find(line_item_id)
    current_item.quantity += 1
    current_item
  end
  end
  </code></pre>
  
  <p>As requested, here is the view code (relevant portion):</p>
  
  <pre><code>&lt;% if @cart %&gt;
  &lt;%= hidden_div_if(@cart.line_items.empty?, id:'cart') do %&gt;
  &lt;div class="row-fluid"&gt;
          &lt;a class="btn btn-success menu" id="menubutton" href="&lt;%= cart_path(session[:cart_id]) %&gt;"&gt;View Cart&lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="row-fluid"&gt; 
                  You have &lt;%= pluralize(@total_current_items, "item") %&gt;in your cart.    
                  &lt;/div&gt;
  
                  &lt;% end %&gt;           
              &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I've tried putting the following in the application helper, but it's not working. It either comes up with an undefined method/variable error message, or says 'You have 0 items in your cart', even when there are items in there. I've tried putting @total_items, total_items etc. to reference it in the view, but I'm new to rails and don't know what to do to get it to work! </p>
  
  <pre><code> def total_items
     @line_items = LineItem.find(params[:id])
     @total_items = @line_items.to_a.sum { |item| item.total_quantity}
   end
  </code></pre>
  
  <p>Where am I going wrong?</p>