# Session usage

**URL:** <https://www.ruby-forum.com/t/session-usage/69013>\
**Category:** Rails\
**Created:** [February 16, 2006, 1:34am UTC](https://www.ruby-forum.com/t/session-usage/69013 "2006-02-16T01:34:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 16, 2006, 1:34am UTC](https://www.ruby-forum.com/t/session-usage/69013/1 "2006-02-16T01:34:14Z")

</div>

Clearly I have the session and can see that session[:user\_id] = 7

Now I want to use the results of this…in a page…so I am trying to  
use the instance variable

@sess\_user

and at the top of the application controller, I have either…

@sess\_user = User.find([:first, “id = ?”, session[:user\_id])  
or  
@sess\_user = User.find([:first, “id = ?”, session[:user\_id])

gives me an error…symbol as Array index

Clues?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 16, 2006, 1:37am UTC](https://www.ruby-forum.com/t/session-usage/69013/2 "2006-02-16T01:37:15Z")

</div>

Hi,

@sess\_user = User.find(:first, :conditions=\>[“id = ?”,  
session[:user\_id])

Eric

Craig W. wrote:

> or  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Eric G.  
[http://www.ericgoodwin.com](http://www.ericgoodwin.com)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 16, 2006, 1:52am UTC](https://www.ruby-forum.com/t/session-usage/69013/3 "2006-02-16T01:52:26Z")

</div>

Yeah…I got brackets out of whack…

@sess\_user = User.find(:first,  
:conditions =\> [“id = ?”, session[:user\_id] ])

or

@sess\_user = User.find(session[:user\_id])

both still return the same error…

symbol as array index

which probably still isn’t what I want to get out of it since I actually  
want user.name from the ‘find’ but nevertheless…the error is still the  
same.

Thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 16, 2006, 1:43am UTC](https://www.ruby-forum.com/t/session-usage/69013/4 "2006-02-16T01:43:18Z")

</div>

On Feb 15, 2006, at 4:39 PM, Tom M. wrote:

> > @sess\_user = User.find([:first, “id = ?”, session[:user\_id])  
> > or  
> > @sess\_user = User.find([:first, “id = ?”, session[:user\_id])
> > 
> > gives me an error…symbol as Array index
> 
> You don’t need the [] around the arguments in find…and there’s  
> a shortcut that you don’t know about.

Correction:

You have the [ in the wrong place. Needs to be after the comma.

Short form is better still…

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 16, 2006, 1:40am UTC](https://www.ruby-forum.com/t/session-usage/69013/5 "2006-02-16T01:40:17Z")

</div>

On Feb 15, 2006, at 4:33 PM, Craig W. wrote:

> or  
> @sess\_user = User.find([:first, “id = ?”, session[:user\_id])
> 
> gives me an error…symbol as Array index

You don’t need the [] around the arguments in find…and there’s  
a shortcut that you don’t know about.

@sess\_user = User.find(session[:user\_id])

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 16, 2006, 2:37am UTC](https://www.ruby-forum.com/t/session-usage/69013/6 "2006-02-16T02:37:43Z")

</div>

Craig,

Can you show us the exact contents of your session, or show us the  
code for the user id being added to the session data? It sounds like  
there is a problem with session[:user\_id]

Estelle.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 16, 2006, 3:47am UTC](https://www.ruby-forum.com/t/session-usage/69013/7 "2006-02-16T03:47:09Z")

</div>

On Wed, 2006-02-15 at 17:34 -0800, Estelle W. wrote:

> Craig,
> 
> Can you show us the exact contents of your session, or show us the  
> code for the user id being added to the session data? It sounds like  
> there is a problem with session[:user\_id]

* * *

I’m ashamed to tell you that I solved it entirely differently. When I  
create the session, I get all the session variables I will need later  
rather than resorting to having to get them out of the db later. This is  
a simple process and it was my twisted logic that said…OK, I need this  
value now and the only way I can get it is from the user\_id in the  
session.

Much simpler - thanks for the concern. I am now onto rights based roles  
and I can see that Chad’s ‘Recipes’ is gonna pay me dividends now.

Thanks

Craig
