Hello, What is the best way in rails to check a session hash value for nil ? I have a session hash called user (session[:user]) that sometimes I have to check for a certain variable like session[:user].email. however the following always results in an error if session[:user].email and I always have to do if session[:user] and session[:user].email Is there a cleaner way to do this then check for nil first everytime and then a value inside the hash second ? I should also mention that session[:user] is assigned the User object record from the DB. thanks adam -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060419/6ea581ca/attachment.html
On 4/18/06, Adam Denenberg <straightflush@gmail.com> wrote:> Hello, > > What is the best way in rails to check a session hash value for nil ? I > have a session hash called user (session[:user]) that sometimes I have to > check for a certain variable like session[:user].email. however the > following always results in an error > > if session[:user].email > > and I always have to do > > if session[:user] and session[:user].email > > Is there a cleaner way to do this then check for nil first everytime and > then a value inside the hash second ? I should also mention that > session[:user] is assigned the User object record from the DB. > > thanks > adam >One nice way is to make a "null" User object for ''anonymous or invalid user'', and make sure that gets set into the session in a before_filter. That way you don''t have to worry about it being nil; you just need to check its permissions the way you''d normally do for a user. Anonymous users simply get no permissions.
Wilson Bilkovich wrote:> On 4/18/06, Adam Denenberg <straightflush@gmail.com> wrote: >> >> if session[:user] and session[:user].email >> >> Is there a cleaner way to do this then check for nil first everytime and >> then a value inside the hash second ? I should also mention that >> session[:user] is assigned the User object record from the DB. >> >> thanks >> adam >> > > One nice way is to make a "null" User object for ''anonymous or invalid > user'', and make sure that gets set into the session in a > before_filter. That way you don''t have to worry about it being nil; > you just need to check its permissions the way you''d normally do for a > user. Anonymous users simply get no permissions.Another way would be to ensure something is entered into session[:user].email everytime? If nothing is entered, then you put a space inside or wat, so later if you see a space you know is empty? -- Posted via http://www.ruby-forum.com/.
Adam Denenberg wrote:> What is the best way in rails to check a session hash value for nil ?{whatever} if session[:key]> I have a session hash called user (session[:user]) that sometimes I have > to check for a certain variable like session[:user].email. however the > following always results in an error > > if session[:user].email > > and I always have to do > > if session[:user] and session[:user].email > > Is there a cleaner way to do this then check for nil first everytime and > then a value inside the hash second ? I should also mention that > session[:user] is assigned the User object record from the DB.It''s usually better to keep the user_id in the session instead of the user object. It avoids issues of trying to keep the session object in sync with the DB version. You can use a before_filter to check that the user_id is set and load the user model into a @user instance variable. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.