views:

1853

answers:

3
+3  Q: 

Rails 2.3 session

Hi,

I am developing a rails 2.3.2 app. I need to keep session_id for an order record, retrieve it and finally delete the session_id when the order is completed. It worked when I used cookies as session store but it doesn't for active_record store. (I restarted my browser, so no cache issue.)

I know rails 2.3 implements lazy session load. I read some info about it but am still confused.

Can somebody clarify how I use session_id for such a case?

What I am doing is...

A user make an order going through several pages.
There is no sign-up, neither login.
So I keep session_id in the order record so that no other user can access the order.
@order = Order.last :conditions => {:id => params[:id], :session_id => session[:session_id] }
When the order is finished, I set nil to session_id column.

How would you implement such a case in lazy session(and active_record store) environment?

Thanks.

Sam

+6  A: 

I ran into the same problem programming a shopping cart. There are actually two issues. As of rails 2.3, sessions are lazy loaded and session[:session_id] no longer works. You must access the session before you can work with it.

puts request.session_options[:id]
session[:session_id] # this forces load of the session in Rails 2.3.x
puts request.session_options[:id]

This outputs:

nil
78eb1e371f3378ed98874f9ce372d652

Update: session.session_id has been deprecated, so use request.session_options[:id] instead.

Update 2: This will be fixed in 3.x (not 2.3.5) https://rails.lighthouseapp.com/projects/8994/tickets/2268-rails-23-session%5Foptionsid-problem

Greg

Greg Benedict
Thank you for the deprecation warning on session.session_id Greg !
Dr1Ku
A: 

I could use that patch to 2.3.5

http://github.com/rails/rails/commit/e0f1a7dc191ffebc9f6cadb6232e567fee8aa491

dont add the Hash inheritence to CookieStore though

A: 

I prefer to use this method to log Session ID (but Rails 2.2 store Session ID in log file as well without any modifications)

application_controller.rb
  before_filter :log_session_id

  private
  def log_session_id
    session[:session_id]
    logger.info "Session ID: " + request.session_options[:id]
  end
end
mikhailov