Forum: Ruby on Rails How do I allow multiple shopping carts per user?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
B3187843b9430ce16974b136ad3fdcf2?d=identicon&s=25 Jake Hawkes (Guest)
on 2007-04-14 20:38
(Received via mailing list)
I'm creating an order entry application based on the depot application
Dave Thomas's book "Agile Web Development 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
process of creating an order, drag and drop any number of line_items
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
session variable. However, when I have two orders open at the same time
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
than updating a hidden input in the form. Any suggestions would be


PS The AJAX code was taken from the shopping cart demo on
Db303dc84d03a992b33cd3ac978f89ae?d=identicon&s=25 Benjamin Curtis (Guest)
on 2007-04-14 21:05
(Received via mailing list)
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?

Meet up at RailsConf:
B3187843b9430ce16974b136ad3fdcf2?d=identicon&s=25 Jake Hawkes (Guest)
on 2007-04-14 22:02
(Received via mailing list)
Ben, thanks for your quick response.

But how do I tell the carts apart? How would I make sure the correct
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

It sounds like maybe I need to give each order a unique identifier
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
are added to it? I could store that id in a hidden input and then use
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

Thanks in advance for the help. I'm just beginning to learn Ruby and
but I'm sure enjoying it!
Db303dc84d03a992b33cd3ac978f89ae?d=identicon&s=25 Benjamin Curtis (Guest)
on 2007-04-15 04:38
(Received via mailing list)
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?

Meet up at RailsConf:
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-04-15 05:33
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.

This topic is locked and can not be replied to.