Caching of models?

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

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. :slight_smile:

Have fun
Tom

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.

Problem solved.
olegf