# Caching of models?

**URL:** <https://www.ruby-forum.com/t/caching-of-models/51082>\
**Category:** Rails\
**Created:** [December 25, 2005, 10:37pm UTC](https://www.ruby-forum.com/t/caching-of-models/51082 "2005-12-25T22:37:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oleg\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ee7513/32.png) [@Oleg\_F](https://www.ruby-forum.com/u/Oleg_F)\
**Post date:** [December 25, 2005, 10:37pm UTC](https://www.ruby-forum.com/t/caching-of-models/51082/1 "2005-12-25T22:37:39Z")

</div>

Hello, people!

I have a problem. I googled, asked for help on IRC. I can’t find an  
answer. Suppose I have model Cart (from “Agile Web D. with  
Rails”). I added call to logger.warn(“Some warning”) to one of it’s  
methods. It fails with NameError, because ‘logger’ is unknown to  
model.

undefined local variable or method `logger’ for #\<Cart:0x36b3bd8  
@items=[], @total\_price=0.0\>

Then I deleted offending line. Hit Refresh. Same error. Cleared  
session cookies. Tried again. Same error. Looks like model is cached.  
Restarted WebRick. Now it’s OK until next syntax error in model.

I’m running rails 1.0.0, ruby 1.8.2 on windows xp sp2. I use webrick  
in development mode.

Why Rails behaves like that? What can I do to prevent this kind of  
caching?

–  
olegf

---

<div class="post-metadata">

**Author:** ![Oleg\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ee7513/32.png) [@Oleg\_F](https://www.ruby-forum.com/u/Oleg_F)\
**Post date:** [December 26, 2005, 5:59am UTC](https://www.ruby-forum.com/t/caching-of-models/51082/2 "2005-12-26T05:59:20Z")

</div>

Oleg F. wrote:

> Then I deleted offending line. Hit Refresh. Same error. Cleared  
> session cookies. Tried again. Same error. Looks like model is cached.  
> Restarted WebRick. Now it’s OK until next syntax error in model.
> 
> I’m running rails 1.0.0, ruby 1.8.2 on windows xp sp2. I use webrick  
> in development mode.
> 
> Why Rails behaves like that? What can I do to prevent this kind of  
> caching?  
> Yep, that exactly what rails did. You have to restart Webrick every time  
> that you modify model or helper.

However, you might be about to control this behavior to some extend.  
Check  
the /config/environment.rb. The RAILS\_ENV is  
control this behavior, say in production mode, Webrick always cached the  
models, controllers, and the helpers. If you modifly one of those files,  
you have to restart the server.

In the development mode, it’s suppose “not to” cache it. I develop using  
Apache in linux, and I have no problem with caching. I just modifly the  
code, and refresh the page. However, when I try with Webrick, I need to  
restart the server anyway, even in development mode.

The only drawback of using Apache and development mode is that it’s a  
bit  
slower. If most of the time you are modifly views, you should not have  
any  
problem them. 🙂

Have fun  
Tom

---

<div class="post-metadata">

**Author:** ![Oleg\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ee7513/32.png) [@Oleg\_F](https://www.ruby-forum.com/u/Oleg_F)\
**Post date:** [December 26, 2005, 10:53am UTC](https://www.ruby-forum.com/t/caching-of-models/51082/3 "2005-12-26T10:53:47Z")

</div>

Thanks for your tip!

Today I have found errata for my version of book. David said, it is  
bug in Rails. There are two workarounds. You can either  
require\_dependency “cart” to the top of store\_controller.rb.  
Or include the “model :cart” line in application\_controller.rb.

> **[Pragmatic Bookshelf: By Developers, For Developers](https://pragprog.com)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

Problem solved.  
olegf
