Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 2.3 session

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

like image 724
Sam Kong Avatar asked Jun 24 '09 00:06

Sam Kong


2 Answers

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_optionsid-problem

Greg

like image 200
Greg Benedict Avatar answered Oct 20 '22 10:10

Greg Benedict


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
like image 3
Anatoly Avatar answered Oct 20 '22 08:10

Anatoly