Rails 4 session.id occasionally nil Rails 4 session.id occasionally nil heroku heroku

Rails 4 session.id occasionally nil


I found the answer, it looks like Rails is trying to be efficient by only creating a session if there is something to store. So accessing the session.id without storing something doesn't return consistent results.

You need to force the session to be created by storing something in it.

TLDR: Add this somewhere before you access the session ID.

session[:foo] = "bar"

Source: http://www.gani.com.au/2013/08/force-session-creation-in-rails/