Hello !
I have bought book Agile web development with Ruby on Rails and i have
come to the section: sessions.

I am having some problems figuring out what does each line of code do.
So i would be gratefull if somoeone would explain it to me.

First I have this:

session[:cart] ||=

Ok i understand the second part which creates new if there isnt an
existing one. but the firt part is what i dont understand.


:cart is because of the name of the model cart.rb ?
Can i add more models ? Something like session[:cart, :person] ?

One more thing i dont understand is this line


in add_to_cart function.
I understand in general that it finds the id but where can i find more
documentation about params and how it is used ?

I have a link but it seems to me like it
isnt full ? I check for params cant find it, check for session
something small and unusuable...

Please help me ! I want to learn but im stuck :frowning:

Hi Eric,

Product.find(params[:id]) selects the record whose primary key value
matches the id passed in the parameters.

So, if your url looks like http://railsapp/product/show/1,
params[:id] would be 1, and the corresponding record gets selected.

The symbol, :cart isn't used because it matches the name of the
model. You can use any name for the session variables and can also
save multiple values in a session.


Thank you very much fr your fast reply.
Could you tell me the correct syntax for saving multiple values to a
session ?

Though, I've used sessions till now, I'm confident that you could do
something like this..

session[:user] = User.find('eric')
session[:cart] = Cart.find_and_add('item1')

- Chaitanya

Do you know where can i check this ?
I havent find any good documentation on session :frowning:

It is not a good practice to save objects into the session for several reasons. One is that you are unnecessarily impacting the performance, you also will run into problems when you have changed your model but the sessions consists of the older version models, which is very difficult to debug.

So instead of storing the object you can store the id of the object in the session.
describes the available session methods.