# How to setup a Session and use its data from other classes?

**URL:** <https://www.ruby-forum.com/t/how-to-setup-a-session-and-use-its-data-from-other-classes/140855>\
**Category:** Rails\
**Created:** [June 19, 2008, 9:40am UTC](https://www.ruby-forum.com/t/how-to-setup-a-session-and-use-its-data-from-other-classes/140855 "2008-06-19T09:40:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaselai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@shaselai](https://www.ruby-forum.com/u/shaselai)\
**Post date:** [June 19, 2008, 9:40am UTC](https://www.ruby-forum.com/t/how-to-setup-a-session-and-use-its-data-from-other-classes/140855/1 "2008-06-19T09:40:16Z")

</div>

I am trying to do an authentication where I have :  
session[“User”] = User.authenticate(@params[“name”],  
@params[“password”])

and the program continues on if the authentication is correct. Now if  
later on in the session I want to access “User”'s data (using active  
directory) I try to use:

if session[“User”].id == 0  
//do something  
else  
//do something else

Now I always get id =0 so i must have set the session wrong or not at  
all? Here is how i do the authentication:

if session[“User”] = User.authenticate(@params[“name”],  
@params[“password”])  
if session[“return\_to”]  
redirect\_to\_path(session[“return\_to”])  
session[“return\_to”] = nil  
else  
redirect\_to :controller =\> “NewController”  
end  
else  
flash[:error] = ‘Invalid user name and/or password.’  
redirect\_to :action =\> “index”  
end

I try to use session[“User”] elsewhere to get data but no good. Can  
someone help? thanks!

---

<div class="post-metadata">

**Author:** ![shaselai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@shaselai](https://www.ruby-forum.com/u/shaselai)\
**Post date:** [June 19, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/how-to-setup-a-session-and-use-its-data-from-other-classes/140855/2 "2008-06-19T15:28:12Z")

</div>

First question is “Does your User.authenticate return a user object to  
be stored in session?”

From the sparse look we have at the code, that’s where I’d be looking  
first…

---

<div class="post-metadata">

**Author:** ![shaselai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@shaselai](https://www.ruby-forum.com/u/shaselai)\
**Post date:** [June 19, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/how-to-setup-a-session-and-use-its-data-from-other-classes/140855/3 "2008-06-19T15:50:15Z")

</div>

No, the User.authenticate does :  
User.find(first,…) to check for user authentication. How do I  
return the object? with this way? sorry i am bit new at this…  
thanks!

---

<div class="post-metadata">

**Author:** ![shaselai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@shaselai](https://www.ruby-forum.com/u/shaselai)\
**Post date:** [June 19, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/how-to-setup-a-session-and-use-its-data-from-other-classes/140855/4 "2008-06-19T16:34:07Z")

</div>

it also seems the ID is 0,1 and apparently in ruby that is true/false  
class… so after I set the session correctly do I just do :

if session[“User”].id == true  
//do something  
else  
//etc…  
?
