Session usage


#1

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


#2

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


Eric G.
http://www.ericgoodwin.com


#3

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


#4

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…


#5

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.


#6

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.


#7

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