How do you clear a single entry from a ruby on rails session?

Jim Soho picture Jim Soho · Mar 26, 2009 · Viewed 20.8k times · Source

In ruby on rails when doing session[:foo] = nil it leaves an entry named :foo in the session object. How can you get rid of that single entry from the session object?

Answer

Daniel Beardsley picture Daniel Beardsley · Feb 9, 2011

It looks like the simplest version works. All stores (Cookie, File, ActiveRecord, ...) use AbstractStore::SessionHash as the object that contains the data, the different stores provide only the means to load and save the AbstractStore::SessionHash instances.

AbstractStore::SessionHash inherits from Hash, so this defers to the Hash#delete method:

session.delete(:key_to_delete)