Emptying cart

hi.
i’m new to ruby on rails
I have a big problem
I’m writing a program wich has a Product class(active record) wich
contains the product I want to sell…and a Cart class where i save the
Products I want to buy…each product has a availability field that i
decrement when I add the product to the cart…
Now
if I empty the cart by clicking a link I can empty the cart and restore
the right availability for each product that was in the cart before
but if I close the browser with the cart not empty, it gets empty, but
the availability is not restored correctly

Is there a way to fix this problem? is there a way to ‘intercept’ the
termination of the browser? or an event that is called at the
application terminate? or a decontructor method for my class Cart?

i red something browsing the web but I was unable to fix this problem

please help me
thanks