Newbie alert! diff between @session['user'].id and @session


#1

I struggled for sometime to use @session[:user].id while using
LoginGenerator to retrieve id of the logged in user. In the end I could
make it work with @session[‘user’].id. Shouldn’t the two approaches be
same?

thanks.


#2

Hi, you should be able to use script console to determine if they are
equivalent. For example,

irb(main):001:0> a = {}
=> {}
irb(main):002:0> a[:this] = ‘foo’
=> “foo”
irb(main):003:0> a[:this]
=> “foo”
irb(main):004:0> a[‘this’] = ‘foo2’
=> “foo2”
irb(main):005:0> a[:this]
=> “foo”
irb(main):006:0> a[:this] == a[‘this’]
=> false

Good luck,

-Conrad


#3

Ok, makes sense… Thanks…
i think loginGenerator by default generates the code that use quotes
for array reference.


#4

Actually, this may be misleading in the case of session, params, and
other certain ‘special’ hashes that Rails uses. Rails has a
“HashWithIndifferentAccess” that allows accesing something by symbol
and key, so that session[:foo] == session[‘foo’]. I’m not positive
that session uses that hash, but you should test this right in a
controller and not just in a plain irb session.

  • Rob

#5

A note: accessing the session with @session[] has been deprecated for
a while now
(a few months). Try using just ‘session[:user]’.

–Matt

On 1/25/07, newtorails. removed_email_address@domain.invalid wrote:


Matt J.
removed_email_address@domain.invalid
President/Technical Director, Acme Art Company (acmeartco.org)


#6

Matt, changing @session to session[…] doesn’t change the situation…
let me know if you see anything different.


#7

Hi, my test using IRB was consistent with my test using sessions
within a controller. Thus, the following isn’t equal:

session[:user] != session[‘user’]

In general, they implemented session as global hash within Rails.

-Conrad