# Rails 2.1 cache problem

**URL:** <https://www.ruby-forum.com/t/rails-2-1-cache-problem/143987>\
**Category:** Rails\
**Created:** [July 24, 2008, 12:47pm UTC](https://www.ruby-forum.com/t/rails-2-1-cache-problem/143987 "2008-07-24T12:47:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dmitrij\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dfb087/32.png) [@Dmitrij\_S](https://www.ruby-forum.com/u/Dmitrij_S)\
**Post date:** [July 24, 2008, 12:47pm UTC](https://www.ruby-forum.com/t/rails-2-1-cache-problem/143987/1 "2008-07-24T12:47:05Z")

</div>

Hi,

class User \< ActiveRecord::Base  
def self.get\_cache(id)  
Rails.cache.fetch(“users/#{id}”) { find(id) }  
end  
end

Accesing a cached model object immediately after caching works as  
expected:

User.get\_cache(1).cache\_key # =\> OK

But once I try to do the same from another action it’s not possible  
anymore.  
I get an error message “stack level too deep”.

User.get\_cache(1).cache)key # =\>

SystemStackError (stack level too deep):  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/  
active\_record/attribute\_methods.rb:64:in `generated_methods?' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/attribute_methods.rb:237:in`method\_missing’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/  
active\_record/attribute\_methods.rb:245:in `method_missing' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/base.rb:2167:in`cache\_key’  
/app/controllers/users\_controller.rb:5:in `index' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/base.rb:1162:in`send’

What am I doing or thinking wrong?

---

<div class="post-metadata">

**Author:** ![Dmitrij\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dfb087/32.png) [@Dmitrij\_S](https://www.ruby-forum.com/u/Dmitrij_S)\
**Post date:** [July 24, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/rails-2-1-cache-problem/143987/2 "2008-07-24T15:06:48Z")

</div>

take a look at: [Can't models be cached in development env? - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/157364)

you enable class caching changing “config.cache\_classes”  
to true in your config/development.rb file.

---

<div class="post-metadata">

**Author:** ![Dmitrij\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dfb087/32.png) [@Dmitrij\_S](https://www.ruby-forum.com/u/Dmitrij_S)\
**Post date:** [July 25, 2008, 12:09pm UTC](https://www.ruby-forum.com/t/rails-2-1-cache-problem/143987/3 "2008-07-25T12:09:03Z")

</div>

It’s look like a bug to reload environment each time after code edit…  
Any other ideas?

Thanks for the response.

---

<div class="post-metadata">

**Author:** ![Dmitrij\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dfb087/32.png) [@Dmitrij\_S](https://www.ruby-forum.com/u/Dmitrij_S)\
**Post date:** [September 17, 2008, 10:20pm UTC](https://www.ruby-forum.com/t/rails-2-1-cache-problem/143987/4 "2008-09-17T22:20:46Z")

</div>

Any update on this caching bug? It’s driving me crazy. Please, post  
a solution if you’ve figured this out, or know of a resource that will  
help me understand it better. Thanks.

(from  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/c74a74df959c56ee](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/c74a74df959c56ee))

These seem to be related:

[http://dev.rubyonrails.org/ticket/10896](http://dev.rubyonrails.org/ticket/10896)  
[http://dev.rubyonrails.org/ticket/10722](http://dev.rubyonrails.org/ticket/10722)

> **[\#115 Caching in Rails 2.1 - RailsCasts](http://railscasts.com/episodes/115-caching-in-rails-2-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

[http://zilkey.com/2008/7/5/rails-cache-memcached-development-mode-and](http://zilkey.com/2008/7/5/rails-cache-memcached-development-mode-and)…

But I still haven’t seen a definitive solution.
