Retreiving Session Variables

Hey Everybody,

I’m new to RoR and I’m awfully confused on how to retrieve data from
sessions. I do know session variables are stored in a hash.

Anyway, I have two models - Note and User

In User, I have a method called login that populates session[:user] –
I also got fedup, and tried storing just the id of the user in
session[:user_id]…

In Note, I want to retreive the ID of the user who is logged in.

Forgive me if what I’m doing is stupid, but I always get the error:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]

Here’s my code in note.rb

class Note < ActiveRecord::Base
def self.my_notes
temp_email = @session[:user_email]
temp = User.get_id(temp_email)
Note.find_by_sql [“SELECT * FROM notes WHERE user_owner = ?”, temp]
end
end

Please help me :wink:

You can’t access the session from model classes. Session stuff belongs
in the controller

Fred

Hi Brendan,

Brendan Lim wrote:

I always get the error:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]

Here’s my code in note.rb

temp_email = @session[:user_email]

It’s possible that your problem results from using the deprecated
@session
That is now treated as an instance variable which will only have a value
in
the request-response cycle in which you assign it a value. Try changing
@session[:user_email] to session[:user_email] in both the assignment and
access statements.

hth,
Bill