# Conditional delete of a session

**URL:** <https://www.ruby-forum.com/t/conditional-delete-of-a-session/155705>\
**Category:** Rails\
**Created:** [December 15, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/conditional-delete-of-a-session/155705 "2008-12-15T19:35:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 15, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/conditional-delete-of-a-session/155705/1 "2008-12-15T19:35:19Z")

</div>

Rails 2.2.2  
Ruby 1.8.6

ActiveRecord Session Store enabled

For testing purposes I wish to ensure that no session exists. To  
accomplish this I wrote:

session.delete if session

But this fails if session == nil and I suspect that it does not work as  
I anticipate if session exists. In the console I see this behaviour:

$ script/console  
Loading development environment (Rails 2.2.2)

> > app.session  
> > NoMethodError: You have a nil object when you didn’t expect it!  
> > The error occurred while evaluating nil.session  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/test\_process.rb:429:in  
> > `session’  
> > from (irb):1

> > app.session.delete if app.session  
> > NoMethodError: You have a nil object when you didn’t expect it!  
> > The error occurred while evaluating nil.session  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action\_controller/test\_process.rb:429:in  
> > `session’  
> > from (irb):2

> > app.get “/”  
> > =\> 200

> > app.session  
> > =\> #\<CGI::Session:0xb723ba24  
> > @dbman=#\<CGI::Session::CookieStore:0xb723b8e4 @original=nil, …

> > app.session.delete if app.session  
> > =\> []

> > app.session  
> > =\> #\<CGI::Session:0xb723ba24  
> > @dbman=#\<CGI::Session::CookieStore:0xb723b8e4 @original=nil,

Now, I believed that nil == false and that the construct “object.method  
if object” should work. Evidently I am misinformed or there is  
something exceptional about session that I do not know.

How is what I desire, to remove the session if one already exists,  
accomplished?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 18, 2008, 2:39am UTC](https://www.ruby-forum.com/t/conditional-delete-of-a-session/155705/2 "2008-12-18T02:39:01Z")

</div>

James B. wrote:

> But this fails if session == nil and I suspect that it does not work as  
> from (irb):1  
> =\> 200  
> @dbman=#\<CGI::Session::CookieStore:0xb723b8e4 @original=nil,
> 
> Now, I believed that nil == false and that the construct “object.method  
> if object” should work. Evidently I am misinformed or there is  
> something exceptional about session that I do not know.
> 
> How is what I desire, to remove the session if one already exists,  
> accomplished?

Perhaps

```
session.delete if defined?(session) && session

```

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
