# Session objects

**URL:** <https://www.ruby-forum.com/t/session-objects/76147>\
**Category:** Rails\
**Created:** [November 16, 2006, 9:53am UTC](https://www.ruby-forum.com/t/session-objects/76147 "2006-11-16T09:53:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 16, 2006, 9:53am UTC](https://www.ruby-forum.com/t/session-objects/76147/1 "2006-11-16T09:53:01Z")

</div>

I store my Member model object in the session, and the Member model is  
linked to various other models such as:

has\_many :orders  
has\_many :invoices

Now, not only does my session contain the info that I want (member  
name, email, etc.) but also all these other linked Models that I do NOT  
want.

Is there any way to turn this behavior off and store ONLY the Member  
model and not it’s relationships? I prefer to store the whole object  
rather than just member\_id which might be my only option…

Thanks in advance,  
Chad

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 16, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/session-objects/76147/2 "2006-11-16T15:29:56Z")

</div>

Hi Chad,

You can set orders and invoices to nil before storing the member in the  
session.  
But be carefull if you want to update the member to the database!

Cheers, Simon

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 16, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/session-objects/76147/3 "2006-11-16T18:06:57Z")

</div>

Simons right you would even want to create a function in your model that  
does the save/retrieve from session after setting these to nil for you

ie

def save\_to\_session  
model.association = nil  
session[:model] = model  
end

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 16, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/session-objects/76147/4 "2006-11-16T18:20:46Z")

</div>

Here’s the thing, if I do:

@member = session[“member”]  
@orders = @member.orders

That automatically stores all of the Order items in my session object.  
Is there any way to call @member.orders without that session behavior?

I COULD do it this way:

@member = session[“member”]  
@orders = Order.find(:all, :conditions =\> [“member\_id = ?”,  
@member.id])

but that defeats the purpose of nice ORM code.

On Nov 16, 9:06 am, Keynan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 16, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/session-objects/76147/5 "2006-11-16T18:34:50Z")

</div>

On 11/16/06, Chad [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there any way to turn this behavior off and store ONLY the Member  
> model and not it’s relationships? I prefer to store the whole object  
> rather than just member\_id which might be my only option…

If you use ActiveRecordStore for your sessions in 1.1.6, associations  
are  
cleared before saving the session.

In 1.2 this is true for all the session stores (thanks to removed\_email\_address@domain.invalid).

jeremy

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 16, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/session-objects/76147/6 "2006-11-16T18:46:53Z")

</div>

Chad wrote:

> Is there any way to turn this behavior off and store ONLY the Member  
> model and not it’s relationships? I prefer to store the whole object  
> rather than just member\_id which might be my only option…
> 
> Thanks in advance,  
> Chad

What’s your reasoning for wanting to store the entire object, rather  
than just the id? I recall reading somewhere that there’s  
little/nothing to be gained performance wise with such practices.  
@member = Member.find(session[:member\_id]) is pretty straight forward.  
So, unless you’re goal is simply to give yourself a migraine writing  
code to disable normal /appropriate Rails behavior… 😉

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 16, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/session-objects/76147/7 "2006-11-16T19:00:56Z")

</div>

I need access to the member object during every request, so I was  
trying to avoid that extra SQL call… wouldn’t there be a performance  
gain in a high-traffic site?

But you’re right… it’s becoming a migraine.

---

<div class="post-metadata">

**Author:** ![Chad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@Chad](https://www.ruby-forum.com/u/Chad)\
**Post date:** [November 16, 2006, 9:43pm UTC](https://www.ruby-forum.com/t/session-objects/76147/8 "2006-11-16T21:43:43Z")

</div>

Chad wrote:

> I need access to the member object during every request, so I was  
> trying to avoid that extra SQL call… wouldn’t there be a performance  
> gain in a high-traffic site?
> 
> But you’re right… it’s becoming a migraine.

Actually, no. Marshalling/Unmarshalling the object from/to the session  
with every hit is more expensive than simply querying the database. Add  
to that the extra code you’ve got to add everywhere to make sure that  
the session[:member] stays current with it’s twin in the database, and  
that if you ever change the internal structure of Member, that you’ve  
got to remember to blow away all the sessions when you deploy the new  
version, etc, etc.

A quick google search turned up this gem.  
[http://blog.inquirylabs.com/2006/05/12/rails-gotcha-associations-stored-in-the-session/](http://blog.inquirylabs.com/2006/05/12/rails-gotcha-associations-stored-in-the-session/)
