Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

rails 2.3 session_options[:id] problem

#2268

I've updated my rails project to work with rails 2.3 I did the tweaks I needed and all is ok except request.session_options[:id]. I want to log request.session_options[:id] but I get a nil value, however after I call the inspect method on request.session ... I no longer get a nil value for request.session_options[:id].
Example
puts request.session_options[:id] #=> nil
request.session.inspect
puts request.session_options[:id] #=> 'b7a45b2bcc227cc34b2b58603822d91c'

Reported by Boban · March 17th, 2009 @ 11:16 AM

State: resolved
Milestone: 3.0.2
Assigned to: josh josh
Importance: none

Activity

  1. Simone Carletti
    Simone Carletti

    I get the same error but running functional tests.

    I've the following method

    
    def something
    user_cookie = cookies[:user_cookie] || session.session_id
    end
    

    changing the method definition to

    
    def something
    user_cookie = cookies[:user_cookie] || request.session_options[:id]
    end
    

    causes the following error.

    
    NoMethodError: You have a nil object when you didn't expect it!
    You might have expected an instance of ActiveRecord::Base.
    The error occurred while evaluating nil.[]
    app/controllers/application_controller.rb:38:in `something'
        test/functional/about_controller_test.rb:16:in `test_index'
    

    March 17th, 2009 @ 05:09 PM

  2. Pawel
    Pawel
    • Tag changed from 2.3, rails, request.session:id, session.session_id to 2.3, rails, request.session_options:id, session.session_id

    This is because of session lazy loading.

    You can work around this by loading the session first and then read its id.

    
    def something
      session[:foo]
      user_cookie = cookies[:user_cookie] || request.session_options[:id]
    end
    

    However I think there should be a way to read session id without loading the session itself (think of database backed sessions) -- see #2177 Unneccessary session loading/creation in form_authenticity_token.

    March 18th, 2009 @ 03:08 PM

  3. edbond2
    edbond2
    • Assigned user set to josh
    • Tag changed from 2.3, rails, request.session_options:id, session.session_id to 2.3, cookie, rails, request, session, test

    June 30th, 2009 @ 06:57 AM

  4. josh
    josh
    • State changed from new to open

    I suppose trying to read the session id should touch the cookie store.

    Can someone try to create a patch for this?

    Also, this should only affect the cookie store. For an example, with the AR store, we don't want to load the session because we already know the session id.

    July 9th, 2009 @ 07:32 PM

  5. josh
    josh
    • State changed from open to incomplete

    July 23rd, 2009 @ 03:08 AM

  6. josh
    josh
    • Assigned user cleared.

    August 19th, 2009 @ 03:55 PM

  7. John Pignata
    John Pignata

    I'm not sure how to retrieve the ID without unmarshaling the cookie data and it seems reasonable to me to load this data if the id is requested so I've created a patch to do that. This should apply to 2-3-stable. Feedback welcomed - not sure what the best way is to fix this.

    August 19th, 2009 @ 05:41 PM

  8. josh
    josh
    • Milestone cleared.

    CookieStore should probably wrap session.options with a lazy hash that triggers the unmarshaling.

    August 31st, 2009 @ 10:20 PM

  9. John Pignata
    John Pignata

    Thanks, Josh. Here's another go at it - against master this time.

    September 2nd, 2009 @ 03:30 AM

  10. josh
    josh
    • Assigned user set to josh
    • State changed from incomplete to open

    September 2nd, 2009 @ 03:39 AM

  11. Repository
    Repository
    • State changed from open to resolved

    (from [e0f1a7dc191ffebc9f6cadb6232e567fee8aa491]) If session_options[:id] is requested when using CookieStore, unmarshal the session to access it [#2268 rails 2.3 session_options[:id] problem state:resolved]

    Signed-off-by: Joshua Peek josh@joshpeek.com
    http://github.com/rails/rails/commit/e0f1a7dc191ffebc9f6cadb6232e56...

    September 3rd, 2009 @ 05:50 PM

  12. John Pignata
    John Pignata

    Looking at your commit, it seems like I made an error when testing the bugfix. CookieStore should be deriving from Hash. I've made a patch to revert this unnecessary change.

    September 3rd, 2009 @ 06:29 PM

  13. Repository
  14. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to

    October 15th, 2010 @ 11:01 PM