# Session variables

**URL:** <https://www.ruby-forum.com/t/session-variables/131719>\
**Category:** Rails\
**Created:** [March 12, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/session-variables/131719 "2008-03-12T18:54:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [March 12, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/session-variables/131719/1 "2008-03-12T18:54:27Z")

</div>

Is there a way to concatenate a number onto a string and use that string  
in a session variable? I tried something like…

x = 1  
session[“user” + x] = user one

but nothing happen unless I use the session[:user] syntax. Is there a  
way around this so I can set the session[:user + x] variable?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [March 12, 2008, 7:02pm UTC](https://www.ruby-forum.com/t/session-variables/131719/2 "2008-03-12T19:02:56Z")

</div>

Chris H. wrote:

> 

session[“user#{x}”.to\_sym]

FYI, it’s generally not recommended to store whole objects in the  
session. It’s much better to store just the ID and retrieve the object  
from the database when you need it.

session[:user\_id] = @user.id

then in future page views

User.find(session[:user\_id])

–  
[http://www.5valleys.com/](http://www.5valleys.com/)  
[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [March 12, 2008, 7:08pm UTC](https://www.ruby-forum.com/t/session-variables/131719/3 "2008-03-12T19:08:52Z")

</div>

> FYI, it’s generally not recommended to store whole objects in the  
> session. It’s much better to store just the ID and retrieve the object  
> from the database when you need it.

Thanks a lot. I am not using it for a whole object. I tried to make  
the example as simple as possible. Thanks for the advice though.
