Ruby on Rails
HowtoWorkWithSessions (Version #34)

Sessions in 30 seconds1

Setting a session value2:

session[:greeting] = "Hello world!"

Reading a session value:

session[:greeting] # => "Hello world!"

Deleting a single session value:

session[:greeting] = nil

Completely removing a previously-set key/value pair3:

[not possible]

Clearing an entire session:

reset_session

Gotchas with sessions

  • Strange application errors – If you put something dodgy into the session (e.g. an unaccessible object) then whichever webserver you running may refuse to run the main dispatcher. Removing all session data should fix it. See TipsAndTricks for session deletion information
  • Model must be available to Sessions – If you store a class (like an active record model) in a session variable, like this
    session[:user] = User.find_by_name('Foo Bar')

    then rails may crash as soon as you move to another controller. Rails becomes confused that there is no immediate class definition for the data in the session. The solution is to put this in application.rb:
    model :user

    Replace " :user " with the name of the model being stored in your sessions, this way it wil load the definition before figuring out the session data.

A more in depth explanation available at HowtoAvoidSessionRestoreError

  • Items added to Session hash not retained – Your browser must be configured to accept cookies from the server. Sounds stupid and obvious but I wasted one whole day wondering why data that I put in the hash in one method wasn’t available in another.

3 Key/Value pair removing workaround

There can be possible a workaround IF

  1. lot of session variables going to be used and we worry about the amount of resources going to be allocated
  2. we like our session information to be easy to read, manage and debug
  3. we need more control over session variables we created
  • have your variables inside a session variable hash, as far as I know we cannot delete the created session variable, but the items inside our variable can be maniupulated the way we like to do so
  • add, modify, delete those items inside the session variable… keeping your session variables clean and more meanigfull than ending up with lot of unwanted session variables in case we create randomly named key/value pairs for some reason
  • this is just a workaround when we need to have variables in our session and also need to have more control over them

More information

  • sessions — explains the basics of sessions in Rails

See also

Questions/Noob remarks

  • I couldn’t access the “session” object in the constructor of my controller. Is it normal ?
    
    def initialize(*params)
    [...]
    session[:my_variable] = 60
    [...]
    

gave


You have a nil object when you didn't expect it !
...
occured while evaluating nil.[]=

Um. What are you even doing using the constructor anyway? Surely before_filter is the way.


1 section shamelessly lifted from >DHH email

2 “session[:foo]” (a method call) is preferred to “@session[:foo]” (an instance variable) although you will often see the latter.

Sessions in 30 seconds1

Setting a session value2:

session[:greeting] = "Hello world!"

Reading a session value:

session[:greeting] # => "Hello world!"

Deleting a single session value:

session[:greeting] = nil

Completely removing a previously-set key/value pair3:

[not possible]

Clearing an entire session:

reset_session

Gotchas with sessions

  • Strange application errors – If you put something dodgy into the session (e.g. an unaccessible object) then whichever webserver you running may refuse to run the main dispatcher. Removing all session data should fix it. See TipsAndTricks for session deletion information
  • Model must be available to Sessions – If you store a class (like an active record model) in a session variable, like this
    session[:user] = User.find_by_name('Foo Bar')

    then rails may crash as soon as you move to another controller. Rails becomes confused that there is no immediate class definition for the data in the session. The solution is to put this in application.rb:
    model :user

    Replace " :user " with the name of the model being stored in your sessions, this way it wil load the definition before figuring out the session data.

A more in depth explanation available at HowtoAvoidSessionRestoreError

  • Items added to Session hash not retained – Your browser must be configured to accept cookies from the server. Sounds stupid and obvious but I wasted one whole day wondering why data that I put in the hash in one method wasn’t available in another.

3 Key/Value pair removing workaround

There can be possible a workaround IF

  1. lot of session variables going to be used and we worry about the amount of resources going to be allocated
  2. we like our session information to be easy to read, manage and debug
  3. we need more control over session variables we created
  • have your variables inside a session variable hash, as far as I know we cannot delete the created session variable, but the items inside our variable can be maniupulated the way we like to do so
  • add, modify, delete those items inside the session variable… keeping your session variables clean and more meanigfull than ending up with lot of unwanted session variables in case we create randomly named key/value pairs for some reason
  • this is just a workaround when we need to have variables in our session and also need to have more control over them

More information

  • sessions — explains the basics of sessions in Rails

See also

Questions/Noob remarks

  • I couldn’t access the “session” object in the constructor of my controller. Is it normal ?
    
    def initialize(*params)
    [...]
    session[:my_variable] = 60
    [...]
    

gave


You have a nil object when you didn't expect it !
...
occured while evaluating nil.[]=

Um. What are you even doing using the constructor anyway? Surely before_filter is the way.


1 section shamelessly lifted from >DHH email

2 “session[:foo]” (a method call) is preferred to “@session[:foo]” (an instance variable) although you will often see the latter.