Okay, I have some stuff working but I''m not exactly sure WHY it''s working. Hopefully someone can help me as I''d like to get it straighted out before I progress too far... Once a user has logged into the system (using salted_login) the associated model gets stored in the session via: @session[''user''] = User.authenticate(@params[''user''][''login''], @params[''user''][''password'']) Later, if I want to access that user model, I can just reference it in my controller like: @user = @session[''user''] or, am I supposed to: @user = @session[:user] ? What exactly is the difference? And, am I right that that is the actual User object being stored in the session and not just its values? Thanks, Mike
Mike Evans wrote:> Okay, I have some stuff working but I''m not exactly sure WHY it''s > working. Hopefully someone can help me as I''d like to get it > straighted out before I progress too far... > > Once a user has logged into the system (using salted_login) the > associated model gets stored in the session via: > @session[''user''] = User.authenticate(@params[''user''][''login''], > @params[''user''][''password'']) > Later, if I want to access that user model, I can just reference it in > my controller like: > @user = @session[''user''] > or, am I supposed to: > @user = @session[:user] ? > > What exactly is the difference? And, am I right that that is the > actual User object being stored in the session and not just its values? >It is just a Hash [1]. It is based on a CGI::Session[2]. The difference is, some_hash[''user''] <- hash on a string some_hash[:user] <- hash on a label Hashes on strings are portable since they are basically {''string1'' => ''string2'', ... }. Hashes on labels might not be portable since how Ruby stores a label in version 1.8.x might be different than how it stores it in 2.2.x or whatever. I might be blowing stuff out of my @55 when I''m talking about portability of the string based hashes, but whatever. :) Hashes on labels might also be faster... In either case, I think, some_hash[''user''] != some_hash[:user] != some_hash[user] So if you store @session[''user''], you have to fetch it with @session[''user''] What is being stored depends on what user *is*. If it is a function that returns 5, then 5 will be stored. If it is a class, then that class will be stored. etc.. - Adam [1] - http://www.ruby-doc.org/core/classes/Hash.html [2] - http://www.ruby-doc.org/core/classes/CGI/Session.html
>> some_hash[''user''] != some_hash[:user] != some_hash[user] >> So if you store @session[''user''], you have to fetch it with@session[''user''] Correct! irb(main):001:0> h = { :fred => ''flintstone'' }=> {:fred=>"flintstone"} irb(main):002:0> h[''fred'']=> nil irb(main):003:0> h[:fred] => "flintstone" irb(main):004:0> -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Adam M. Sent: Friday, 13 May 2005 9:05 AM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] @session noob question Mike Evans wrote:> Okay, I have some stuff working but I''m not exactly sure WHY it''s > working. Hopefully someone can help me as I''d like to get it > straighted out before I progress too far... > > Once a user has logged into the system (using salted_login) the > associated model gets stored in the session via: > @session[''user''] = User.authenticate(@params[''user''][''login''], > @params[''user''][''password'']) > Later, if I want to access that user model, I can just reference it in> my controller like: > @user = @session[''user''] > or, am I supposed to: > @user = @session[:user] ? > > What exactly is the difference? And, am I right that that is the > actual User object being stored in the session and not just itsvalues?>It is just a Hash [1]. It is based on a CGI::Session[2]. The difference is, some_hash[''user''] <- hash on a string some_hash[:user] <- hash on a label Hashes on strings are portable since they are basically {''string1'' => ''string2'', ... }. Hashes on labels might not be portable since how Ruby stores a label in version 1.8.x might be different than how it stores it in 2.2.x or whatever. I might be blowing stuff out of my @55 when I''m talking about portability of the string based hashes, but whatever. :) Hashes on labels might also be faster... In either case, I think, some_hash[''user''] != some_hash[:user] != some_hash[user] So if you store @session[''user''], you have to fetch it with @session[''user''] What is being stored depends on what user *is*. If it is a function that returns 5, then 5 will be stored. If it is a class, then that class will be stored. etc.. - Adam [1] - http://www.ruby-doc.org/core/classes/Hash.html [2] - http://www.ruby-doc.org/core/classes/CGI/Session.html _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
What you''re calling labels are really strings (or alternatively, interned symbols). I wouldn''t worry about ruby versions breaking persistence on something as integral to the language as symbols, and it''s perfectly safe to persist hashes with them. If the persistence format changes then ruby will likely break everything or maintain backwards compatibility. Keep in mind that rails uses yaml anyways and that''s probably even safer from change. There''s been some discussion of this in the past, but I would recommend not storing domain objects in the session in the long term. It can lead to stale data and take up a lot of space. With the cost of a simple PRIMARY KEY select being so low (at least in mysql or any memcache setup), it usually makes sense to store the user id in the session and fetch your user object (for example) on the first use of it. I usually just have this or something like it in my ApplicationController: helper_method :current_user def current_user @current_user ||= User.find(@session[''user'']) if @session[''user''] end and then I can reference current_user from any controller or view. If not logged in, then current_user is nil, else it references the user object, always. Looking at this code, I just realized that it hits the database multiple times if the user is not logged in, but only once if they are. I''ll have to fix that, but you get the idea. Brian Brian On 5/12/05, Neville Burnell <Neville.Burnell-uEDVyssJ3mUpAS55Wn97og@public.gmane.org> wrote:> >> some_hash[''user''] != some_hash[:user] != some_hash[user] > >> So if you store @session[''user''], you have to fetch it with > @session[''user''] > > Correct! > > irb(main):001:0> h = { :fred => ''flintstone'' }=> {:fred=>"flintstone"} > irb(main):002:0> h[''fred'']=> nil > irb(main):003:0> h[:fred] => "flintstone" > irb(main):004:0> > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Adam M. > Sent: Friday, 13 May 2005 9:05 AM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] @session noob question > > Mike Evans wrote: > > > Okay, I have some stuff working but I''m not exactly sure WHY it''s > > working. Hopefully someone can help me as I''d like to get it > > straighted out before I progress too far... > > > > Once a user has logged into the system (using salted_login) the > > associated model gets stored in the session via: > > @session[''user''] = User.authenticate(@params[''user''][''login''], > > @params[''user''][''password'']) > > Later, if I want to access that user model, I can just reference it in > > > my controller like: > > @user = @session[''user''] > > or, am I supposed to: > > @user = @session[:user] ? > > > > What exactly is the difference? And, am I right that that is the > > actual User object being stored in the session and not just its > values? > > > It is just a Hash [1]. It is based on a CGI::Session[2]. > > The difference is, > > some_hash[''user''] <- hash on a string > some_hash[:user] <- hash on a label > > Hashes on strings are portable since they are basically {''string1'' => > ''string2'', ... }. Hashes on labels might not be portable since how Ruby > stores a label in version 1.8.x might be different than how it stores it > in 2.2.x or whatever. I might be blowing stuff out of my @55 when I''m > talking about portability of the string based hashes, but whatever. :) > Hashes on labels might also be faster... > > In either case, I think, > > some_hash[''user''] != some_hash[:user] != some_hash[user] > > So if you store @session[''user''], you have to fetch it with > @session[''user''] > > What is being stored depends on what user *is*. If it is a function that > returns 5, then 5 will be stored. If it is a class, then that class will > be stored. etc.. > > - Adam > > [1] - http://www.ruby-doc.org/core/classes/Hash.html > [2] - http://www.ruby-doc.org/core/classes/CGI/Session.html > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
Thanks everyone. And thanks Brian, that was going to be my next question (whether to store the actual user object or just an id then pull it from the db anytime it''s needed).>There''s been some discussion of this in the past, but I would >recommend not storing domain objects in the session in the long term. >It can lead to stale data and take up a lot of space. With the cost of >a simple PRIMARY KEY select being so low (at least in mysql or any >memcache setup), it usually makes sense to store the user id in the >session and fetch your user object (for example) on the first use of >it. I usually just have this or something like it in my >ApplicationController: > > helper_method :current_user > def current_user > @current_user ||= User.find(@session[''user'']) if @session[''user''] > end > >and then I can reference current_user from any controller or view. If >not logged in, then current_user is nil, else it references the user >object, always. Looking at this code, I just realized that it hits the >database multiple times if the user is not logged in, but only once if >they are. I''ll have to fix that, but you get the idea. > >Brian > > >