Dont use sessions table in rails

I’ve just learned (the hard way ;-) ) that You should not use sessions table in DB. It is linked with some Ruby on Rails internals. Why dont use? Because somewhere in ActionPack is used instance variable @session. Using table named sessions in rails conventions mean that should exist class SessionController, and in it will be using @session to pass data to views (all generated via scaffold). Then crash happens and You will find following error:

NoMethodError in SessionsController#show
undefined method `close' for #<Session:0x8cf658c>

And in framework stack:

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1792:in `method_missing'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:984:in `close_session'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:1026:in `process_cleanup_without_flash'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/flash.rb:147:in `process_cleanup_without_filters'


Lets examine line 984 in actionpack/action_controller/base:

      def close_session
        @session.close unless @session.nil? || Hash === @session

Having local (from SessionControler) variable @session which covers variable defined in actionpack is the issue. And there is no close in this method, what Rails are trying to say…

Solution – dont use sessions table or dont use scaffold code in this case (use other variable name than @session to pass data to views).

And why I call it hard way? Because when I ran on this issue, on my FreeBSD development box I was doing upgrade of Ruby in background. So I was sure that error (strange for newbie like me, isn’t it?) was caused by this upgrade. I took me over 2 hours to figure where exactly problem is…

4 thoughts on “Dont use sessions table in rails

  1. Pingback: NetManiac/My road with Ruby on Rails » Blog Archive » Column names restrictions

  2. Ha, thanks for doing this short writeup… I suspected the problem was related to ruby’s internal use of “sessions” or “session,” most unfortunate that I now have a lot of changes to make.

  3. Ah, thanks! I’ve been spending the last hour trying to figure this one out. Good ol’ google!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.