Forum: Ruby on Rails Caching of models?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Oleg F. (Guest)
on 2005-12-25 23:37
(Received via mailing list)
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
Anocha Yimsiriwattana (Guest)
on 2005-12-26 06:59
(Received via mailing list)
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 <application directory>/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
Oleg F. (Guest)
on 2005-12-26 11:53
(Received via mailing list)
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.
http://books.pragprog.com/titles/rails/errata

Problem solved.
olegf
This topic is locked and can not be replied to.