Forum: Ruby on Rails session usage

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.
Craig W. (Guest)
on 2006-02-16 02:34
(Received via mailing list)
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
Eric G. (Guest)
on 2006-02-16 02:37
(Received via mailing list)
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
Tom M. (Guest)
on 2006-02-16 02:40
(Received via mailing list)
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.
Tom M. (Guest)
on 2006-02-16 02:43
(Received via mailing list)
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...
Craig W. (Guest)
on 2006-02-16 02:52
(Received via mailing list)
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
Estelle W. (Guest)
on 2006-02-16 03:37
(Received via mailing list)
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.
Craig W. (Guest)
on 2006-02-16 04:47
(Received via mailing list)
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
This topic is locked and can not be replied to.