Re: session question..

Frederick Cheung
Fri, 10 Oct 2008 14:06:19 -0000

On Oct 10, 2:56 pm, Jay Pangmi
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:
> Frederick Cheung wrote:
> > This doesn''t seem to make any sense. For example the
@cart_items array
> > will only ever contain 1 item, you are storing the whole cart object
> > in the session (what''s the point of storing it in the
database and the
> > session ?)
>
> session[:cart]=Cart.new(cartobj), what is this doing actually I''m
not
> sure?

Your code actually says
session[:cart] ||= Cart.new(cartobj)

what this does is:
- if session[:cart] is set, do nothing
- if not create a new instance of cart and store it in the session.

> As you said just to store carts id in the session, how can I do
> that? and while emptying the cart how can I get all the carts'' id
stored
> in the session and delete the records with that id from the carts table?
> With my current way, how can I validate individual details coz currently
> its all coming as params[:cart] coz first I have to validate and if
> alright save in the database.
>
You would do something like like
cart = Cart.create(...)
session[:cart_id] = cart.id

When you need to access the cart later you would do
cart = Cart.find session[:cart_id]
and then do stuff with the instance of cart (eg destroy it)

> > Actually if you check your code you''ll find that beyond the
first time
> > you call add_to_cart it won''t actually do anything.
>
> can you please show me how? I''m not yet familiar with rails code.
>
> > Is you cart object actually an item in a cart (rather than the
> > container, as the name usually implies) ?
>
> My cart object (params[:cart]) is all the values passed from the browser
> by the customer like date, time etc and this values I''m saving to
the
> database table. Hope this is what you trying to know.

That''s not what I was asking. Some of what you were saying was
implying that a customer would have multiple carts at the same time,
whereas normally a person only ever has one cart (possibly containing
more than one thing). In that case you may not want to be persisting
the cart but the items in the cart (which might be what you''re trying
to do already, but calling them carts just makes everything rather
confused).

Fred
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com