# ActiveRecord and memory\_store sessions error

**URL:** <https://www.ruby-forum.com/t/activerecord-and-memory-store-sessions-error/137209>\
**Category:** Rails\
**Created:** [May 10, 2008, 2:16am UTC](https://www.ruby-forum.com/t/activerecord-and-memory-store-sessions-error/137209 "2008-05-10T02:16:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brubix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f19dbf/32.png) [@Brubix](https://www.ruby-forum.com/u/Brubix)\
**Post date:** [May 10, 2008, 2:16am UTC](https://www.ruby-forum.com/t/activerecord-and-memory-store-sessions-error/137209/1 "2008-05-10T02:16:48Z")

</div>

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](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 %\>

---

<div class="post-metadata">

**Author:** ![Brubix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f19dbf/32.png) [@Brubix](https://www.ruby-forum.com/u/Brubix)\
**Post date:** [May 10, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/activerecord-and-memory-store-sessions-error/137209/2 "2008-05-10T19:24:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Brubix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f19dbf/32.png) [@Brubix](https://www.ruby-forum.com/u/Brubix)\
**Post date:** [May 10, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/activerecord-and-memory-store-sessions-error/137209/3 "2008-05-10T22:04:16Z")

</div>

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 ?
