AWDWR Depot App / Ruby question

Yes I’m one of those folks learning ruby by way of Rails … I’m
confounded
by the cart …

I understand this

def find_cart
session[:cart] ||= Cart.new
end

Then called as

@cart = find_cart

What I don’t understand is what’s actually returned here …

I keep expecting to see:

session[:cart] = cart

Somewhere to save the cart to the session but I don’t see that anywhere.

Does the find_cart method return a reference to the session variable?
Do
all methods return references? …

But I guess where I’m hung up on is is it returning the value of
session[:cart], ie, a cart object with the value of the cart, or is it
returning a reference to that actual session[:cart]?

I’m missing the magic of how the actions taken on the ‘cart’ are
actually
reflected in the session[:cart] persistent between calls.

Fluffy Hippo wrote:

@cart = find_cart

What I don’t understand is what’s actually returned here …

I keep expecting to see:

session[:cart] = cart

Somewhere to save the cart to the session but I don’t see that anywhere.

The ||= is what does the assignment of a new empty cart to the session
if it doesn’t already have a cart. Written out very verbosely that does:

if session[:cart]
return session[:cart]
else
session[:cart] = Cart.new
return session[:cart]
end

The add_to_cart described later on is what adds individual items to the
cart.


Michael W.

On Jan 4, 2007, at 3:46 PM, Fluffy Hippo wrote:

But I guess where I’m hung up on is is it returning the value of
session[:cart], ie, a cart object with the value of the cart, or is
it returning a reference to that actual session[:cart]?

I’m missing the magic of how the actions taken on the ‘cart’ are
actually reflected in the session[:cart] persistent between calls.

The session contains a reference to the cart. Because Rails store
away everything referenced from the session at the end of a request,
any updates to the cart during that request will be saved.

Dave

Fluffy Hippo wrote:

But I guess where I’m hung up on is is it returning the value of
session[:cart], ie, a cart object with the value of the cart, or is it
returning a reference to that actual session[:cart]?

I’m missing the magic of how the actions taken on the ‘cart’ are actually
reflected in the session[:cart] persistent between calls.

The book talks briefly about sessions and how they are stored
persistently in that section on carts or you can skip to the reference
section and read more about them there.

session[:cart] returns whatever object is associated with that key, in
this case a Cart object, sort of like this:

irb(main):015:0> a = { :id => 1, :name => “blah” }
=> {:name=>“blah”, :id=>1}
irb(main):016:0> a[:name]
=> “blah”
irb(main):017:0> session = Hash.new
=> {}
irb(main):018:0> session[:cart] = a
=> {:name=>“blah”, :id=>1}
irb(main):019:0> session[:cart]
=> {:name=>“blah”, :id=>1}
irb(main):020:0> session[:cart].object_id
=> 23456251850360
irb(main):021:0> a.object_id
=> 23456251850360
irb(main):022:0> session.object_id
=> 23456251831800


Michael W.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs