# Where to initialize session variables?

**URL:** <https://www.ruby-forum.com/t/where-to-initialize-session-variables/154327>\
**Category:** Rails\
**Created:** [November 27, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/where-to-initialize-session-variables/154327 "2008-11-27T18:54:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b782af/32.png) [@Jej](https://www.ruby-forum.com/u/Jej)\
**Post date:** [November 27, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/where-to-initialize-session-variables/154327/1 "2008-11-27T18:54:56Z")

</div>

Hi all,

I would like to initialize some session variables when the user visits  
the app for the first time. Currently I do it in an application  
before\_filter (escaped if the session already exists), but that occurs  
for each request, it’s dirty.

Do you have any better solution?

Jej

---

<div class="post-metadata">

**Author:** ![Jej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b782af/32.png) [@Jej](https://www.ruby-forum.com/u/Jej)\
**Post date:** [November 27, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/where-to-initialize-session-variables/154327/2 "2008-11-27T20:05:34Z")

</div>

The solution you have seems like a fine answer to me. Simple,  
standard, and should be pretty easy for any incoming developer to  
understand. If youve got a method like the following then it probably  
isn’t a big performance drain either.

before\_filter :intialize\_session\_stuff  
def intialize\_session\_stuff  
return if session\_stuff\_is\_already\_set?  
…  
end
