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…
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]).
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…
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?
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.