Forum: Ruby on Rails best way to check session for nil

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Adam D. (Guest)
on 2006-04-19 06:01
(Received via mailing list)
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
Wilson B. (Guest)
on 2006-04-19 06:19
(Received via mailing list)
On 4/18/06, Adam D. <removed_email_address@domain.invalid> 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.
Bao L. (Guest)
on 2006-04-19 06:33
Wilson B. wrote:
> On 4/18/06, Adam D. <removed_email_address@domain.invalid> 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?
Josh S. (Guest)
on 2006-04-19 08:40
Adam D. 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 S.
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.