# How do I allow multiple shopping carts per user?

**URL:** <https://www.ruby-forum.com/t/how-do-i-allow-multiple-shopping-carts-per-user/92026>\
**Category:** Rails\
**Created:** [April 14, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/how-do-i-allow-multiple-shopping-carts-per-user/92026 "2007-04-14T20:38:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakedh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jakedh](https://www.ruby-forum.com/u/jakedh)\
**Post date:** [April 14, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/how-do-i-allow-multiple-shopping-carts-per-user/92026/1 "2007-04-14T20:38:49Z")

</div>

I’m creating an order entry application based on the depot application  
in  
Dave T.'s book “Agile Web D. with Rails”, except one major  
difference is that I want the user to be allowed to have any number of  
orders open at a time. The idea is that a sales representative will, in  
the  
process of creating an order, drag and drop any number of line\_items  
onto  
the order and using AJAX the items on the order will be updated. At this  
point, the order has not yet been saved so I’m storing the line\_items in  
a  
session variable. However, when I have two orders open at the same time  
the  
session variable gets applied to both orders, and any items added to one  
order are also added to the other.

That said, I need to find an alternative solution and I’m wondering what  
would be the best way to do this? I’m hoping there’s a more elegant  
solution  
than updating a hidden input in the form. Any suggestions would be  
appreciated.

Jake

PS The AJAX code was taken from the shopping cart demo on  
[script.aculo.us](http://script.aculo.us)

---

<div class="post-metadata">

**Author:** ![jakedh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jakedh](https://www.ruby-forum.com/u/jakedh)\
**Post date:** [April 14, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/how-do-i-allow-multiple-shopping-carts-per-user/92026/2 "2007-04-14T21:05:50Z")

</div>

If you want to keep cart and item info in the session as you  
currently have it, you can just change the cart item in the session  
hash to an array, so you could have an array of carts, each with its  
own set of items.

–  
Building an e-commerce site with Rails?  
[http://agilewebdevelopment.com/rails-ecommerce](http://agilewebdevelopment.com/rails-ecommerce)

Meet up at RailsConf:  
[http://railsconf2007.conferencemeetup.com/](http://railsconf2007.conferencemeetup.com/)

---

<div class="post-metadata">

**Author:** ![jakedh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jakedh](https://www.ruby-forum.com/u/jakedh)\
**Post date:** [April 14, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/how-do-i-allow-multiple-shopping-carts-per-user/92026/3 "2007-04-14T22:02:53Z")

</div>

Ben, thanks for your quick response.

But how do I tell the carts apart? How would I make sure the correct  
cart  
ends up with the correct order since the orders don’t have any unique ID  
yet? I’m not set on storing the item info in the session if there’s a  
good  
alternative.

It sounds like maybe I need to give each order a unique identifier  
before  
the user adds any items or saves the order, and that way I can use your  
suggestion and create a hash of carts with the order id as the key.

So would you recommend that I have a value stored in the database that  
increments every time an order is started but before it’s saved or and  
lines  
are added to it? I could store that id in a hidden input and then use  
the  
value when I create the order. And if I do this, how would I change the  
“drop\_receiving\_element” to pass the ID along with the product\_id? Am I  
going to need to add a hidden input containing the order\_id to each of  
my  
Draggables?

Thanks in advance for the help. I’m just beginning to learn Ruby and  
Rails,  
but I’m sure enjoying it!

---

<div class="post-metadata">

**Author:** ![jakedh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jakedh](https://www.ruby-forum.com/u/jakedh)\
**Post date:** [April 15, 2007, 4:38am UTC](https://www.ruby-forum.com/t/how-do-i-allow-multiple-shopping-carts-per-user/92026/4 "2007-04-15T04:38:18Z")

</div>

You could store the carts as a hash, keyed by a token or GUID. I  
personally put carts in the database so I get full ActiveRecord  
richness for them (and then convert them to a sale and then a  
shipment later), but some people prefer not to have that kind of  
transient data in the database.

–  
Building an e-commerce site with Rails?  
[http://agilewebdevelopment.com/rails-ecommerce](http://agilewebdevelopment.com/rails-ecommerce)

Meet up at RailsConf:  
[http://railsconf2007.conferencemeetup.com/](http://railsconf2007.conferencemeetup.com/)

---

<div class="post-metadata">

**Author:** ![jakedh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce73a5/32.png) [@jakedh](https://www.ruby-forum.com/u/jakedh)\
**Post date:** [April 15, 2007, 5:33am UTC](https://www.ruby-forum.com/t/how-do-i-allow-multiple-shopping-carts-per-user/92026/5 "2007-04-15T05:33:26Z")

</div>

Hmm, I would recommend creating a new model/class (not AR model) to  
handle your multiple shopping carts. That way, as your website expands,  
you have flexibility with the model and can add things as they arise.  
You then, would of course, put this model into the session. It is much  
cleaner than just keeping a hash in the session, especially if shopping  
carts is an integral part of your website. You want to do it right.

Aryk
