views:

50

answers:

1

Does anyone know how to instruct rails to NOT cache classes that are included within the lib folder?

+3  A: 

By "caching classes" I suppose you mean that source files inside the app directory are automatically reloaded in development environment before a new request is processed?

This is not related to caching, the normal behavior of Ruby is, to read and parse a source file once and never again as long as the process runs. Rails (ActiveSupport::Dependencies actually) provides a mechanism to reload the whole code before a request is processed. In development environment, this is useful since you don't want to restart the local webserver for every change you do to the code. In production environment this would badly hurt performance and is turned off therefore.

By default, the app classes are marked as reloadable. You can mark arbitrary classes to be reloaded before a request is processed in development environment by using the unloadable class method:

class MyClass
  unloadable # mark this class as reloadable before a request is processed

  # …
end

Beware that not every class may play well with unloading. As long as you define your class in one source file that gets found and loaded by Rails' autoloading mechanism, you're probably good. But can run into troubles if you re-open your class elsewhere to monkeypatch it since autoloading won't catch this.

Andreas
Not exactly. What I mean is that when I load a class (using require and include) into my application. Then whenever that class is used on another page or another request, then rails does NOT require the file (load the file) again, it simply uses the class that was loaded before (because it stays in memory). This gets frustrating since I need to reload the rails server each time I make a change to the lib class that is included. I am running it in development mode with the config cache classes set to false.
matsko
I see. The Rails reloading mechanism probably only works if your class is covered by Rails' autoloading, not if you require it manually. I'd suggest to let your class load by Rails autoloading, which is very convenient anyway (you don't have to require it and it works everywhere). If that's not possible, you could try to exploit Rails' autoloading by loading your class file in a file that is covered by autoloading (but make sure to use #load, not #require, since require only loads once)
Andreas