# @cart.add\_product(product)

**URL:** <https://www.ruby-forum.com/t/cart-add-product-product/193891>\
**Category:** Rails\
**Created:** [August 26, 2010, 8:05am UTC](https://www.ruby-forum.com/t/cart-add-product-product/193891 "2010-08-26T08:05:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pravin\_mishra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c37758/32.png) [@pravin\_mishra](https://www.ruby-forum.com/u/pravin_mishra)\
**Post date:** [August 26, 2010, 8:05am UTC](https://www.ruby-forum.com/t/cart-add-product-product/193891/1 "2010-08-26T08:05:39Z")

</div>

Any one can explain me???

in agile web development book in chapter Cart Creation

depot\_f/app/models/cart.rb

class Cart  
attr\_reader :items  
def initialize  
@items = []  
end

def add\_product(product)  
@items \<\< product  
end  
end

depot\_f/app/views/store/index.html.erb

\<%= button\_to ‘Add to Cart’, :action =\> ‘add\_to\_cart’, :id =\> product %\>

depot\_f/app/controllers/store\_controller.rb

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)  
end

when use will click on Add\_to\_cart button in index page then width  
particular product id add\_to\_cart action take placed in  
store\_controller.rb file.After finding the array of row from from  
table product and assign to product variable. then find\_cart method is  
called in same controller which is private and create OR initialize  
session. then add\_product method is called width product variable then  
it will goes to cart.rb model and then execute def

add\_product(product)  
@items \<\< product  
end

please explain me why add\_product(product) method in store\_controller  
will go in Cart class in model cart.rb why not in other class.

thanks

---

<div class="post-metadata">

**Author:** ![pravin\_mishra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c37758/32.png) [@pravin\_mishra](https://www.ruby-forum.com/u/pravin_mishra)\
**Post date:** [August 26, 2010, 12:14pm UTC](https://www.ruby-forum.com/t/cart-add-product-product/193891/2 "2010-08-26T12:14:27Z")

</div>

On 26 August 2010 07:04, pravin mishra [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> depot\_f/app/controllers/store\_controller.rb  
> table product and assign to product variable. then find\_cart method is  
> called in same controller which is private and create OR initialize  
> session. then add\_product method is called width product variable then  
> it will goes to cart.rb model and then execute def
> 
> add\_product(product)  
> @items \<\< product  
> end
> 
> please explain me why add\_product(product) method in store\_controller  
> will go in Cart class in model cart.rb why not in other class.

add\_product is called by the line  
@cart.add\_product(product)  
@cart is a Cart object so it calls the add\_product method of the Cart  
class. Perhaps you should do a bit of reading up on basic Ruby. The  
Ruby Pickaxe book is very good.

Colin
