# How to save an object with its associations into session

**URL:** <https://www.ruby-forum.com/t/how-to-save-an-object-with-its-associations-into-session/116972>\
**Category:** Rails\
**Created:** [October 11, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/how-to-save-an-object-with-its-associations-into-session/116972 "2007-10-11T20:06:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunopreis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brunopreis](https://www.ruby-forum.com/u/brunopreis)\
**Post date:** [October 11, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/how-to-save-an-object-with-its-associations-into-session/116972/1 "2007-10-11T20:06:41Z")

</div>

Hi,

How do I save an object with its children into the session? By default  
AR cleans the children when I put the object there.

And if someone knows any docs about that please show the link.

Thks,

Bruno R.

---

<div class="post-metadata">

**Author:** ![brunopreis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brunopreis](https://www.ruby-forum.com/u/brunopreis)\
**Post date:** [October 11, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/how-to-save-an-object-with-its-associations-into-session/116972/2 "2007-10-11T20:11:40Z")

</div>

What do you mean it cleans by default ?

Start  
session[:user] = User.find(1)

then you can do this  
session[:user].id

or that?  
session[:user].contacts.length

---

<div class="post-metadata">

**Author:** ![brunopreis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brunopreis](https://www.ruby-forum.com/u/brunopreis)\
**Post date:** [October 12, 2007, 12:54am UTC](https://www.ruby-forum.com/t/how-to-save-an-object-with-its-associations-into-session/116972/3 "2007-10-12T00:54:13Z")

</div>

that is what I can’t do: session[:user].contacts.length

On Oct 11, 11:11 am, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![brunopreis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brunopreis](https://www.ruby-forum.com/u/brunopreis)\
**Post date:** [October 12, 2007, 2:42am UTC](https://www.ruby-forum.com/t/how-to-save-an-object-with-its-associations-into-session/116972/4 "2007-10-12T02:42:28Z")

</div>

Bruno R. wrote:

> How do I save an object with its children into the session? By default  
> AR cleans the children when I put the object there.
> 
> And if someone knows any docs about that please show the link.

In general, it’s a _bad_ idea to put a full AR object in the session. It  
mostly works, but it can get you in trouble. The accepted best practice  
is to store the id in the session, then fetch the record from the  
database when you need it. That way you avoid the nasty synchronization  
issues etc.

session[:user\_id] = user.id

then later…

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

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
