# Shopping Cart for production

**URL:** <https://www.ruby-forum.com/t/shopping-cart-for-production/177198>\
**Category:** Rails\
**Created:** [October 13, 2009, 7:56am UTC](https://www.ruby-forum.com/t/shopping-cart-for-production/177198 "2009-10-13T07:56:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [October 13, 2009, 7:56am UTC](https://www.ruby-forum.com/t/shopping-cart-for-production/177198/1 "2009-10-13T07:56:19Z")

</div>

Reading the Agile book there’s an excerpt that states:

‘‘Make the Cart class an Active Record object and store cart data in  
the database. The session would then store the cart object’s id. When  
a request comes in, we’d extract this id from the session and then  
load the cart from the database.’’

This Cart class had the methods below. It initializes an @items array  
which contains CartItem objects (which doesn’t extends from  
ActiveRecord). My guess is that this CartItem class should also be  
changed into an ActiveRecord object eventhough this point is not  
mentioned on the book. Am I right making this assumption?

Thanks!

class Cart

def initialize  
@items = []  
end

def add\_product(product)  
current\_item = @items.find {|item| item.product == product}  
if current\_item  
current\_item.increment\_quantity  
else  
current\_item = CartItem.new(product)  
@items \<\< current\_item  
end  
current\_item  
end

end
