views:

272

answers:

1

I have a class instance variable on one of my AR classes. I set its value at boot with an initializer and, after that, never touch it again except to read from it. In development mode, this value disappears after the first request to the web server. However, when running tests, using the console or running the production server this does not happen.

# The AR class
class Group < ActiveRecord::Base

  class << self
    attr_accessor :path
  end

end

# The initializer
Group.path = File.join(RAILS_ROOT, "public", "etc")

# First request in a view
%p= Group.path #=> "/home/rails/app/public/etc"

# Second request in a view
%p= Group.path #=> nil

Is there something about development mode that nukes instance variables from classes with each request? If so, is there a way to disable this for specific variables or classes?

A: 

In development mode, classes are not cached, which means they are all reloaded on every request. In test and production mode, they are cached which means your class instance varaibles suvives. The caching setting is set in the relevant files in config/environments.

One workaround is to set a global or environment variable in your initializer and then define your class-level accessor to return that value.

Dave Ray