views:

173

answers:

1

When setting session variables using cookie store in Rails 2, I can see the _domain_session variable being set as expected, and it shows up in the cookies in my browser. However, whenever I try to access the request.session hash, it's always empty, except for immediately after setting the session.

  # In a controller
  Rails.logger.debug "BEFORE SESSION_ID: #{request.session_options[:id]}"
  session[:user_id] = fbs[:uid]
  session[:access_token] = fbs[:access_token]
  render :text => '"Success"'
  Rails.logger.debug "SESSION_ID: #{request.session_options[:id]}"

The above code will always show

BEFORE SESSION_ID:     
SESSION_ID: f8db970260cb969156304a421036a8fb

Meaning that the session id is set whenever anything is added into the session hash, but somehow gets reset before the next request. I don't see anything anywhere that should reset the session.

Why can I see the session cookie being set but I can't access the data through the session variable?

A: 

Figured it out. It seems that session loading is lazy.

It won't let you look at the session options until you try to access the session.

If I put

if session.present?
end

above the code, the session gets loaded and I can look at the session_options.

Bizarre.

Jamie Wong