# Models in the session and serialization

**URL:** <https://www.ruby-forum.com/t/models-in-the-session-and-serialization/115049>\
**Category:** Rails\
**Created:** [September 20, 2007, 5:37am UTC](https://www.ruby-forum.com/t/models-in-the-session-and-serialization/115049 "2007-09-20T05:37:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [September 20, 2007, 5:37am UTC](https://www.ruby-forum.com/t/models-in-the-session-and-serialization/115049/1 "2007-09-20T05:37:59Z")

</div>

Hi

What are the repercussions of storing a model in the session with  
respect to the relations being up to date and such? Is the information  
actually serialized or is it loaded / unloaded in between actions?

(I would assume it’s being serialized but I would like to make sure).

Secondly, is this a good practice in Rails or should I be storing ID’s  
instead and loading unloading from the DB myself?(It’s okay in other  
technologies if you manage serialization well but Rails is still new to  
me)

I have some comments on BOTH serialization and load/unload with respect  
to overhead, excessive db connections, netork IO etc etc, but I’ll get  
into that if it’s needed when people reply.

Thanks.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [September 20, 2007, 5:46am UTC](https://www.ruby-forum.com/t/models-in-the-session-and-serialization/115049/2 "2007-09-20T05:46:05Z")

</div>

from personal experience they are serialized and do not work well when  
being stored in the session. your best bet is to keep the id in the  
session and query the db each time

On Sep 19, 10:37 pm, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [September 20, 2007, 6:08am UTC](https://www.ruby-forum.com/t/models-in-the-session-and-serialization/115049/3 "2007-09-20T06:08:51Z")

</div>

Andrew B. wrote:

> from personal experience they are serialized and do not work well when  
> being stored in the session. your best bet is to keep the id in the  
> session and query the db each time
> 
> On Sep 19, 10:37 pm, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Okay obviously this creates nice fresh data every action, but, there may  
be times when I need it to be stale…

I also worry about the excessive queries but I can learn to get around  
it, what really raises my eyebrow (You’re not te first person to tell me  
models in session == no good) is how will Rails handle a decent caching  
mechanism if I always am querying the db?

First thought for both the need for stale info AND caching is a Factory  
Pattern who’s job is to handle the conversion (back and forth) of my  
Model information and ‘flattening’ it into a non ActiveRecord normal  
Class.

I welcome and encourage comments.

Thanks

- 

John
