Getting session_id in rails
If you are using database sessions, rails sessions table stores session information in “sessions” table that contains two columns: “session_id” and “data”.
Now, if you need to know the session_id of current session your first instinct would be to do the following:
session.session_idThis will not work since session is just hash containing name/value pairs and not an ActiveRecord object. So what do we do introspect session’s own information.
Solution: We use “request.session_options”. It is a hash that contains session_id and a few more options related to session.
request.session_options.inspect ={:secure=>false, :secret=>"ZBYULTOIJYLYWVNONXNSWUJCKMVVFPIZCEEGDAIE", :expire_after=>nil, :key=>"_SOMEGT_session", :id=>"4d4999d4650f4b21121c8f2b65917fac", :cookie_only=>true, :httponly=>true, :path=>"/", :domain=>nil}
Here :id is the session_id and :key is the cookie name under which it stores the session_id.
Now I know.
Related posts:
- Ruby Interview Questions I’ve been attending a few Ruby interviews recently. Here are...
- Ruby Authlogic: lazy initialization based on defined?() call What is the difference between following two versions of code?...
- Working with Authlogic in script/console When probing / debugging issues with Authlogic on rails console,...
Related posts brought to you by Yet Another Related Posts Plugin.






Thank you so much for posting this! It is exactly the problem I have been scratching my head about.
Craig Lennox
15 Oct 09 at 6:50 pm