# AWDWR Depot App / Ruby question

**URL:** <https://www.ruby-forum.com/t/awdwr-depot-app-ruby-question/81278>\
**Category:** Rails\
**Created:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/awdwr-depot-app-ruby-question/81278 "2007-01-06T19:30:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fluffy\_Hippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f475e1/32.png) [@Fluffy\_Hippo](https://www.ruby-forum.com/u/Fluffy_Hippo)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/awdwr-depot-app-ruby-question/81278/1 "2007-01-06T19:30:58Z")

</div>

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? …

---

<div class="post-metadata">

**Author:** ![Fluffy\_Hippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f475e1/32.png) [@Fluffy\_Hippo](https://www.ruby-forum.com/u/Fluffy_Hippo)\
**Post date:** [January 6, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/awdwr-depot-app-ruby-question/81278/2 "2007-01-06T19:31:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Fluffy\_Hippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f475e1/32.png) [@Fluffy\_Hippo](https://www.ruby-forum.com/u/Fluffy_Hippo)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/awdwr-depot-app-ruby-question/81278/3 "2007-01-06T19:30:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Fluffy\_Hippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f475e1/32.png) [@Fluffy\_Hippo](https://www.ruby-forum.com/u/Fluffy_Hippo)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/awdwr-depot-app-ruby-question/81278/4 "2007-01-19T16:30:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fluffy\_Hippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f475e1/32.png) [@Fluffy\_Hippo](https://www.ruby-forum.com/u/Fluffy_Hippo)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/awdwr-depot-app-ruby-question/81278/5 "2007-01-19T16:30:11Z")

</div>

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.
