# Are objects stored in session updated automatically?

**URL:** <https://www.ruby-forum.com/t/are-objects-stored-in-session-updated-automatically/127732>\
**Category:** Rails\
**Created:** [January 31, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/are-objects-stored-in-session-updated-automatically/127732 "2008-01-31T20:23:40Z")\
**Posts on this page:** 3\
**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 31, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/are-objects-stored-in-session-updated-automatically/127732/1 "2008-01-31T20:23:40Z")

</div>

If I lookup a user, and store that object in my session

```
  @user = User.find(:id)
  session[:user] = @user

```

then the user record changes in the database in some way, then I try  
and reaccess that object

```
  @user = session{:user]
  @first_name = @user.first_name

```

Is the field “first\_name” the most updated or is it still the value  
originally stored in the object?

- Dave

---

<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 31, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/are-objects-stored-in-session-updated-automatically/127732/2 "2008-01-31T20:39:34Z")

</div>

Hi, if you update a session variable, you’re not updating the database  
as  
well. Thus, in your example, you’re simply modifying the object’s  
field,  
first\_name, stored in session.

Good luck,

-Conrad

---

<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 31, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/are-objects-stored-in-session-updated-automatically/127732/3 "2008-01-31T21:04:27Z")

</div>

The object stored in the session is just a reference to the in-memory  
User object, it will not  
reflect changes to the database. Conventional RoR wisdom dictates  
that you should not  
store full objects in the session at all, but rather the id of the  
object which can then be used to  
lookup the object in the database as necessary.
