session question..

Jay Pangmi
Fri, 10 Oct 2008 08:07:06 -0000

Hi, I''m reading Agile Web Development with Rails 2nd Edition. Here, to
save items a customer has selected, it has used session and a "Cart"
class which is a model but doesn''t have ''carts'' table
in the database.
In the class it has defined method to add selected items as:
--------------------------------------------------------------------------------
def add_product(product)
 @items << product
end
--------------------------------------------------------------------------------
where @items is an array and product is created with:
--------------------------------------------------------------------------------
def add_to_cart
 @cart=find_cart
 product=Product.find(params[:id])
 @cart.add_product(product)
end
--------------------------------------------------------------------------------
and there is a use of find_cart method which is used to find cart or
create new if there isn''t any as:
--------------------------------------------------------------------------------
def find_cart
 session[:cart] ||= Cart.new
end
--------------------------------------------------------------------------------

Now, I''m trying to follow the author but my situation is littly twisted
as in my case customer will enter lots other details than just a
campsite name. For eg: it will be campsite name, arrival_date, days to
stay, group size and also claims for family rate if eligible. So, I''ve
thought of saving all these details in the "carts" table in the
database. So I''ve also made Cart class but I have
''carts'' table as well.
So, I''ve used <%form_for :cart ....%> to get all the details from
the
form in one object and done @cart=find_cart(params[:cart]) and find_cart
method as: (I''m not sure if its right way. I even don''t know
what
exactly is happening between sessions table which was created via
db:session:create and my ''carts'' table.)
--------------------------------------------------------------------------------
def find_cart(cartobj)
 session[:cart] ||= Cart.new(cartobj)
end
--------------------------------------------------------------------------------

so far so good, details got saved but when it came to emptying the cart
list, I''m confused what to do. Author has done it as:
--------------------------------------------------------------------------------
def empty_cart
 session[:cart]=nil
 redirect_to :action => :index
end
--------------------------------------------------------------------------------
If i do the same just the data in the sessions table is being deleted
and the details in the carts table are still there. So, how can I delete
details related with the particular session from this ''carts''
table as
it will contain details from other users as well with different session
id. Thanks for your time to go thru and please advice any
suggestions.... thnx