NetManiac

Witold Rugowski on web20 wave with Ruby on Rails

Dont use sessions table in rails

Posted on May 30, 2006 - Filed Under Ruby

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'

[cut]

Lets examine line 984 in actionpack/action_controller/base:

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

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…

Popularity: 5% [?]

Hits for this post: 2283

Similar Posts

Comments

4 Responses to “Dont use sessions table in rails”

  1. NetManiac/My road with Ruby on Rails » Blog Archive » Column names restrictions on May 30th, 2006 12:05

    [...] dont use sessions table in DB (or don’t use code generated by scaffold) details explained http://nhw.pl/wp/2006/05/30/dont-use-sessions-table-in-rails [...]

  2. Charlie Bowman on June 7th, 2006 23:21

    Thanks for posting this. I’m sure you’re not the only one who has tried to use sessions as a table name!

  3. Leif on June 15th, 2006 0:25

    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.

  4. Mike Rowlands on July 18th, 2006 14:26

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

Leave a Reply