# Shopping cart help

**URL:** <https://www.ruby-forum.com/t/shopping-cart-help/118888>\
**Category:** Rails\
**Created:** [November 1, 2007, 1:06am UTC](https://www.ruby-forum.com/t/shopping-cart-help/118888 "2007-11-01T01:06:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dehlan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dehlan](https://www.ruby-forum.com/u/dehlan)\
**Post date:** [November 1, 2007, 1:06am UTC](https://www.ruby-forum.com/t/shopping-cart-help/118888/1 "2007-11-01T01:06:51Z")

</div>

I’m in the process of learning rails, and am trying to implement  
something kind of like a shopping cart in my application. Basically,  
I want users to be able to kind of have a “wish list” of the products  
in the store. Right now, users can browse through the list of items  
for sale and add them to a cart. I’m stuck at the point of trying to  
grab the list of items from the session and save them for future  
viewing.

I’ve looked at the shopping cart example in Agile Web D. w/  
Rails, which helped get me to the point I’m currently at. However,  
the example of capturing an order doesn’t really fit with what I’d  
like to be able to do. It doesn’t seem as if I need the “order” and  
“line\_items” models, but instead should save the list of products  
under a “wish list” table for the users to come back and look at. I’m  
thinking that it should minimally contain a column for the title of  
the wish list (what the user names it), plus a list of the ids of the  
products they have saved. Any suggestions? Do I need to have an  
equivalent “line\_items” model, or can I just reference the ids of the  
products in my “wish list” table? How do I go about saving that and  
then displaying that in a unique url for the user to browse back to in  
the future? Thanks for the help.

---

<div class="post-metadata">

**Author:** ![dehlan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dehlan](https://www.ruby-forum.com/u/dehlan)\
**Post date:** [November 1, 2007, 7:05am UTC](https://www.ruby-forum.com/t/shopping-cart-help/118888/2 "2007-11-01T07:05:38Z")

</div>

Why wouldn’t you have another link/button that reads “Add to Wishlist”  
and when the buyer clicks on it, the item is added to the list.  
You will need a table for a wishlist with 2 colums: id and customer\_id  
and a table for wishlist\_items with these columns:  
t.column :product\_id, :integer  
t.column :cart\_id, :integer  
t.column :price, :float  
t.column :amount, :integer  
t.column :created\_at, :datetime

then your relationships should be:  
wishlist:  
has\_many :cart\_items  
has\_many :products, :through =\> :cart\_items  
belongs\_to :custome

customer:  
has\_one: wishlist

Then, you can add the items from the session to the wishlist.

How to add the functionality to move the items in the cart to the  
wishlist, I still not sure how to do (new to rails as well).  
But the above works for me, so the items in the cart stay till the  
next time the customer logs in (with the use of an authentication  
system.)

HTH,  
Elle

---

<div class="post-metadata">

**Author:** ![dehlan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dehlan](https://www.ruby-forum.com/u/dehlan)\
**Post date:** [November 1, 2007, 9:12am UTC](https://www.ruby-forum.com/t/shopping-cart-help/118888/3 "2007-11-01T09:12:59Z")

</div>

Check out RailsCart…

[http://groups.google.com/group/railscart?lnk=li](http://groups.google.com/group/railscart?lnk=li)  
[http://code.google.com/p/railscart/](http://code.google.com/p/railscart/)  
[http://railscart.org/](http://railscart.org/)

HTH - Dave P.

---

<div class="post-metadata">

**Author:** ![dehlan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dehlan](https://www.ruby-forum.com/u/dehlan)\
**Post date:** [November 2, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/shopping-cart-help/118888/4 "2007-11-02T16:26:26Z")

</div>

Thanks for the input. What I don’t understand is why there needs to  
be another wishlist\_items table. Wouldn’t that mean that with every  
add to your wishlist there is a new row in a separate table that just  
points products to a wishlist? I just see the wishlist\_items table  
growing to be very large. Is there a way to direct a product directly  
into a wishlist without having to have a middle step?

So basically, is there a way to do it without having to have a  
wishlist\_items point a product id to a wishlist id – could the  
wishlist just contain the list of products itself without an in  
between? It would only really need a user\_id and a product\_list,  
avoiding any redundant entries (like price, quantity, etc which is  
done at checkout anyways).

Thanks again.
