laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org
2008-Jan-31 19:22 UTC
Are objects stored in session updated automatically?
If I lookup a user, and store that object in my session @user = User.find(:id) session[:user] = @user then the user record changes in the database in some way, then I try and reaccess that object @user = session{:user] @first_name = @user.first_name Is the field "first_name" the most updated or is it still the value originally stored in the object? - Dave --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Conrad Taylor
2008-Jan-31 19:39 UTC
Re: Are objects stored in session updated automatically?
Hi, if you update a session variable, you''re not updating the database as well. Thus, in your example, you''re simply modifying the object''s field, first_name, stored in session. Good luck, -Conrad On 1/31/08, laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org <laredotornado-8iDLEspWrrZBDgjK7y7TUQ@public.gmane.org> wrote:> > > If I lookup a user, and store that object in my session > > @user = User.find(:id) > session[:user] = @user > > then the user record changes in the database in some way, then I try > and reaccess that object > > @user = session{:user] > @first_name = @user.first_name > > Is the field "first_name" the most updated or is it still the value > originally stored in the object? > > - Dave > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The object stored in the session is just a reference to the in-memory User object, it will not reflect changes to the database. Conventional RoR wisdom dictates that you should not store full objects in the session at all, but rather the id of the object which can then be used to lookup the object in the database as necessary. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---