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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
simon.martinelli-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-16 14:11 UTC
Re: Session objects
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Pratt <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 11/16/06, Chad <carimura-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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....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 sd-b1jIzdlRSJ1eoWH0uzbU5w@public.gmane.org). jeremy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chad wrote:> 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 >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..... ;-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Nov 16, 9:42 am, Jon Garvin <jgarvin.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Chad wrote: > > 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, > > ChadWhat''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..... ;-)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''ve been toying back and forth with this subject for awhile... Thanks for clearing it up. member_id it is! Chad On Nov 16, 12:34 pm, Jon Garvin <jgarvin.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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-stor...--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---