Session objects

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

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

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

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. [email protected]

On 11/16/06, Chad [email protected] 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 [email protected]).

jeremy

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… :wink:

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.

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/