Rails 2 and broken sessions

My hosting company (bluehost.com) updates their system software now and then without any e-mail notifications to their users. Unfortunately, if you host a Ruby on Rails application (in my case sharepdf.com) each time they upgrade Ruby on Rails, there is a good chance that something will break. Recently, they upgraded to the latest version of Rails (2.0.2) and needless to say, my Rails application stopped working. It turns out that the latest version of Rails has changed the default sessions store from a file store to a cookie store. The problem is that if you stored anything other than a simple data in your session, then to get your application to work, you have to spend a few days porting your application. The fact that Rails upgrade is not backwardly compatible is really annoying. But that is a separate issue.

So what is the easiest way to port your file backed sessions application to Rails 2? The answer is to use the database for your sessions. But keep in mind that Rails 2 no longer uses MySQL as its default database. Here is how I ported my sharepdf.com application to Rails 2.0.2:

  1. Create a brand new rails application using the command rails -d mysql appname
  2. Edit environment.rb file and explicity state that you wish to use database for sessions: simply uncomment this line:
    config.action_controller.session_store = :active_record_store.
  3. Use the command rake db:sessions:create to create the database definition for the sessions.
  4. Use the command rake db:migrate to actually create the database tables (I’m assuming that you have updated the database.yml with correct user name and password and have create the database in MySQL). The rake command will then create the sessions database table.
  5. Copy your controllers, models, views, … from the your current Rails application to this one.
  6. You should be good to go!

2 Responses to “Rails 2 and broken sessions”

  1. sj Says:

    Well all of sudden my Ruby on Rails application on SharePDf.com stopped working. Everytime a session object was needed, the server returned HTTP 500 and the logs showed the following:

    Status: 500 Internal Server Error
    marshal data too short
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session/active_record_store.rb:84:in `load'
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session/active_record_store.rb:84:in `unmarshal'
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session/active_record_store.rb:122:in `data'
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session/active_record_store.rb:303:in `restore'
    /usr/lib/ruby/1.8/cgi/session.rb:304:in `[]‘
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/cgi_process.rb:134:in `session’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/cgi_process.rb:166:in `stale_session_check!’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/cgi_process.rb:114:in `session’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1131:in `assign_shortcuts_without_flash’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/flash.rb:167:in `assign_shortcuts’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:518:in `process_without_filters’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:685:in `process_without_session_management_support’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session_management.rb:123:in `process’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:388:in `process’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:171:in `handle_request’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115:in `dispatch’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:126:in `dispatch_cgi’
    /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:9:in `dispatch’
    dispatch.cgi:10

    This is really annoying, every time my ISP updates some aspect of Ruby on Rails, the application needs to be updated.

  2. Steve Says:

    I also believe it was unwise for the rails committers to not make rails 2 backwards compatible. I am in the middle of a crunch trying to get two websites out the door and I made the mistake of upgrading to rails 2, believing it would be backwards compatible. Joke’s on me!

    Seems like there are some youthful people in charge of rails perhaps without much respect for the developers trying to get things done and make a living in some cases. I fear if they pull one or two more stunts like this they will lose a lot of mature developers.

Leave a Reply