# New Session?

**URL:** <https://www.ruby-forum.com/t/new-session/124804>\
**Category:** Rails\
**Created:** [January 2, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/new-session/124804 "2008-01-02T19:34:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 2, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/new-session/124804/1 "2008-01-02T19:34:40Z")

</div>

Is there a method for determining whether or not the session is a NEW  
session? For example. the first request of a session I want to be  
able to perform an action. Subsequent requests under the same session  
id, I do not want to execute the action.

Thanks!  
Tom

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 2, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/new-session/124804/2 "2008-01-02T19:51:07Z")

</div>

On Jan 2, 2008, at 12:33 PM, TomRossi7 wrote:

> Is there a method for determining whether or not the session is a NEW  
> session? For example. the first request of a session I want to be  
> able to perform an action. Subsequent requests under the same session  
> id, I do not want to execute the action.

I don’t know of a method, but you could do something like

if session[:my\_var].nil?

# it’s a new session

# do some funky stuff

session[:my\_var] = my\_value  
end

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 2, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/new-session/124804/3 "2008-01-02T19:55:49Z")

</div>

You could look if certain session attribute was set or not. In case it  
wasn’t, it was the first time you requested it and you could call that  
action and eventually set this session attribute for the next request:

if session[:first\_time].nil?  
your\_action  
session[:first\_time] = true  
end

Regards

TomRossi7  
escribió:\> Is there a method for determining whether or not the session is a NEW

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 2, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/new-session/124804/4 "2008-01-02T19:59:56Z")

</div>

Thanks for the responses Borja and Phillip! Those methods would  
definitely do what I want, I was just hoping their was a better way to  
do it without creating a new session variable. You would think there  
is a hook somewhere when the session id is created?

– Tom

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 3, 2008, 12:47am UTC](https://www.ruby-forum.com/t/new-session/124804/5 "2008-01-03T00:47:26Z")

</div>

Hah! I’m not concerned with my eternal security, just trying to have  
beautiful code with minimal overhead. The session id is created by  
the rails framework and that is why I was thinking their may be a hook  
or attribute somewhere I could access (kinda like ActiveRecord’s  
new\_record? method).

–Tom

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 2, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/new-session/124804/6 "2008-01-02T23:10:01Z")

</div>

On 1/2/08, TomRossi7 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the responses Borja and Phillip! Those methods would  
> definitely do what I want, I was just hoping their was a better way to  
> do it without creating a new session variable. You would think there  
> is a hook somewhere when the session id is created?

You will not burn in hell for creating additional session variables,  
especially an int. Some people (not me of course) stick entire  
ActiveRecord objects in their sessions without harm.

Besides all that, aren’t _you_ the one creating the session id to  
start with? The line right below there is your opportunity to do your  
one-time-only stuff.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
