title: Shopping Cart functionality, current_cart method definition on Rails 3
body: <p>i´m brand new to Stackoverflow and fairly new to web development in general.
  I´ve been learning Rails for a few months and started to build a simple app to teach myself.</p>
  
  <p>I’ll try to be concise and to the point, the app goes like this:  The user signs up or logs in so he can perform CRUD operations to his own Products and Customers. He can also search for customers by phone number, retrieving either the found customer  or a  form to create a new one. Pretty simple. Now, each customer can place many orders (or carts, whatever you want to call it), which store many products, purchased at a given time by a given customer.
  You can see a working example here, <a href="http://order-checker.herokuapp.com" rel="nofollow">http://order-checker.herokuapp.com</a> with mocked up cart functionality to see how it should all fit together. 
  User: guest, password: abcd1234    </p>
  
  <p>Now I’ve wrote a second example with what I have so far in terms of cart functionality, line items models, controllers, etc  and without the user auth for simplicity. <a href="https://github.com/gatosaurio/mockup" rel="nofollow">https://github.com/gatosaurio/mockup</a>
  . Here are my models for this second example:  </p>
  
  <pre><code>class Customer &lt; ActiveRecord::Base
    attr_accessible :name, :street
    has_many :carts
  end
  
  class Product &lt; ActiveRecord::Base
    attr_accessible :name, :price
  end
  
  class Cart &lt; ActiveRecord::Base
    belongs_to :customer
    has_many :line_items
    attr_accessible :purchased_at
  
    def total_price
      line_items.to_a.sum(&amp;:full_price)
    end
  end
  
  class LineItem &lt; ActiveRecord::Base
    attr_accessible :cart_id, :product_id, :quantity, :unit_price
    belongs_to :cart
    belongs_to :product
  
    def full_price
      unit_price * quantity
    end
  end
  </code></pre>
  
  <p>So, in the customers index action I have a new_cart_path link for each one of these. In carts#new I want a list of all the products , with an “add to order” post link.</p>
  
  <pre><code>  &lt;% @products.each do |product| %&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;%= link_to product.name, product_path(product) %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= form_tag(line_items_path, :method =&gt; "post") do %&gt;
          &lt;%= hidden_field_tag(:product_id, product.id) %&gt;
          &lt;%= submit_tag("Add to Cart") %&gt;&lt;/td&gt;
     &lt;% end %&gt;
  
  
    &lt;/tr&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>The sidebar of this new cart action should also display all the line items associated with this cart, something like this, maybe.  </p>
  
  <pre><code>&lt;% @cart.line_items.each do |line_item| %&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;%= line_item.product.name %&gt;&lt;/td&gt;
      &lt;td class="qty"&gt;&lt;%= line_item.quantity %&gt;&lt;/td&gt;
      &lt;td class="price"&gt;&lt;%= number_to_currency(line_item.unit_price) %&gt;&lt;/td&gt;
      &lt;td class="price"&gt;&lt;%= number_to_currency(line_item.full_price) %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= link_to "Delete", line_item, method: :delete, :limit =&gt; 1,  data: { confirm: 'Are you sure?'}%&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;% end %&gt;
  &lt;tr&gt;
    &lt;td class="total price" colspan="4"&gt;
    &lt;Total: &lt;%= number_to_currency @cart.total_price %&gt;&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>Now, here´s line_items#create</p>
  
  <pre><code>def create
    @product = Product.find(params[:product_id])
    if LineItem.exists?(:cart_id =&gt; current_cart.id, :product_id =&gt; @product.id)
      item = LineItem.find(:first, :conditions =&gt; [ "cart_id = #{current_cart.id} AND product_id = #{@product.id}" ])
          LineItem.update(item.id, :quantity =&gt; item.quantity + 1)
    else  
          @line_item = LineItem.create!(:cart =&gt; current_cart, :product =&gt; @product, :quantity =&gt; 1, :unit_price =&gt; @product.price)
          flash[:notice] = "Added #{@product.name} to cart."
    end
  redirect_to new_cart_path
  end
  </code></pre>
  
  <p>So you see, <strong>my specific question</strong> after all this is: <strong>how would I define the current_cart method in application_controller.rb</strong>? And I ask this because all the resources on shopping cart functionality i´ve been able to find from google searching (<em>Agile Web Development with Rails, 4th Edition</em>) were almost exclusively dealing with creating a session object for the cart instance, but I find this approach unfitting for my case, since I need each cart associated only with one specific customer instead of a session being held on the app user´s  level.</p>
  
  <p>I really hope this to be a valid question, sory if this is too long and my current rep level doesn't allow me to post more than two link, but you can find all the code on my Github account.</p>
  
  <p>Thanks a lot  :)</p>