ActiveRecord and memory_store sessions error

It seems that memory_store sessions have problems storing ActiveRecord
objects.
The following test application work perfectly with the standard
session option (Rails 2.0.2) but if I add
config.action_controller.session_store = :memory_store
to envirnment.rb, I get the following error when I refresh the page:

SystemStackError in Unit#index
Showing unit/index.rhtml where line #1 raised:

stack level too deep

Extracted source (around line #1):

1:

<%= @unit.id %>


2:

<%= @unit.description %>


3:

I’ve found this ticket dating back to 2006 that seems to report a
similar problem:
http://dev.rubyonrails.org/ticket/6035

Any idea other than adopting a different session option ?

Many thanks in advance.
Bruno


model unit.rb
class Unit < ActiveRecord::Base
end

controller unit_controller.rb
class UnitController < ApplicationController

def index
if session[:currentUnit]
@unit = session[:currentUnit]
else
@unit = Unit.find(:first)
session[:currentUnit] = @unit
end
end

end

view index.rhtml

<%= @unit.id %>

<%= @unit.description %>

Brubix wrote:


model unit.rb
class Unit < ActiveRecord::Base
end

controller unit_controller.rb
class UnitController < ApplicationController

def index
if session[:currentUnit]
@unit = session[:currentUnit]
else
@unit = Unit.find(:first)
session[:currentUnit] = @unit
end
end

end

view index.rhtml

<%= @unit.id %>

<%= @unit.description %>

I don’t think you want to be storing the whole object to session.

def index
if session[:current_unit_id]
@unit = Unit.find(session[:current_unit_id])
else
@unit = Unit.find(:first)
session[:current_unit_id] = @unit.id
end
end

but you’d probably prefer to do it with a before_filter.

Thank you for your answer.
Could you please illustrate it in a little more detail ?

In the real application I’m working on, changes to the current unit
may occur in more than one user interaction and are not necessarily
comitted to the db at the end of work.

Of course I can switch to a different session engine (what I already
did) or change the application logic but still my questions are:

  • is this the expected behavior of the :memory_store or is it a bug ?
  • is there something I should check or set to change this behavior ?