# RE: current user's id?

**URL:** <https://www.ruby-forum.com/t/re-current-users-id/94715>\
**Category:** Rails\
**Created:** [May 11, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/re-current-users-id/94715 "2006-05-11T22:44:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathandelane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@nathandelane](https://www.ruby-forum.com/u/nathandelane)\
**Post date:** [May 11, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/re-current-users-id/94715/1 "2006-05-11T22:44:55Z")

</div>

JP,

It depends on what authorization mechanism you are using, but if the  
complete user info is stored in the session it would be something  
like…

```
session[:user].id

```

or, if you are just storing the id in the session and doing a lookup for  
each access to user data…

```
session[:user]

```

substitute your session variable for the “user” entry.

Hope that helps.

Nathan

---

<div class="post-metadata">

**Author:** ![nathandelane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@nathandelane](https://www.ruby-forum.com/u/nathandelane)\
**Post date:** [May 12, 2006, 12:11am UTC](https://www.ruby-forum.com/t/re-current-users-id/94715/2 "2006-05-12T00:11:49Z")

</div>

If you mean the OS User-Id, there isn’t one. You could use cookies and  
sessions and  
assign an id. Or do you mean a database id unique key?

Warren S.

---

<div class="post-metadata">

**Author:** ![nathandelane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@nathandelane](https://www.ruby-forum.com/u/nathandelane)\
**Post date:** [May 12, 2006, 12:30am UTC](https://www.ruby-forum.com/t/re-current-users-id/94715/3 "2006-05-12T00:30:49Z")

</div>

Warren S. wrote:

> If you mean the OS User-Id, there isn’t one. You could use cookies and  
> sessions and  
> assign an id. Or do you mean a database id unique key?
> 
> Warren S.

I meant the id in the ‘users’ table.  
This works for me: @session[‘user’].id

Out of curiosity I looked at the complete ‘user’ hash:

@session[‘user’]

And it returned the groups that the user is a member of. And I cannot  
find where this information is placed in the session.

Is it normal for the user to include the group members from a  
many-to-many relation (ie. users \<-- groups\_users --\> groups)?

Thanks  
JP

---

<div class="post-metadata">

**Author:** ![nathandelane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@nathandelane](https://www.ruby-forum.com/u/nathandelane)\
**Post date:** [May 12, 2006, 3:17am UTC](https://www.ruby-forum.com/t/re-current-users-id/94715/4 "2006-05-12T03:17:23Z")

</div>

You should be careful about storing the user object in the session.  
It’s associations will be cached from the first time they are  
accessed. Either just store the id and query the user each request, or  
be very sure to reload the stored object and associations when  
necessary.

I prefer the approach of storing the user id in the session and  
querying it each request.

class User \< ActiveRecord::Base  
cattr\_accessor :current  
end

Then populate User.current with each request:

class ApplicationController \< ActionController::Base  
before\_filter do  
User.current = User.find(session[:user\_id])  
end  
end

Then you can use User.current anywhere to get the currently logged in  
user.

-Jonathan.
