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 ;) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
You can''t access the session from model classes. Session stuff belongs in the controller Fred -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---