uninitialized constant oddities

hi there!

unfortunately, we are repeatedly having problems with uninitialized
constants in our rails app (v1.1.6), which is running on a mongrel
cluster behind apache's mod_proxy_balancer.

---- snip ----
NameError (uninitialized constant Base):
    /app/models/image.rb:90:in `subclasses'
    /app/models/image.rb:98:in `subclass?'
    /app/models/image.rb:261:in `count'
    /app/controllers/image_controller.rb:95:in `list'
---- snip ----

"Base" is actually "Image::Base", defined in lib/image/base.rb:
class Image::Base.

immediately after a mongrel_cluster restart it's working for a few
requests, though. (approx. 4 requests, resulting from 4 mongrel
servers being run?) the strange thing just is that everything works
fine when run from script/console ... except when commenting out the
"require 'image/base'" from config/environment.rb! -- how come?

code details are as follows:

app/controllers/image_controller.rb: class ImageController <

---- snip ----
  def list
    # Image.count is where all evil originates
    @images = ::Paginator.new(Image.count, 10) { |offset, per_page|
      Image.find(:all, :limit => per_page, :offset => offset)
---- snip ----

app/models/image.rb: class Image < Entity (Entity < ActiveRecord::Base):

---- snip ----
  def count
    return super if subclass?

    subclasses.inject(0) { |sum, klass|
      sum += klass.count

  def subclass?
    subclasses.include? self

  # here's where the uninitialized constant comes in
  def subclasses(table_must_exist = true)
    Image::Base.subclasses table_must_exist
---- snip ----

any ideas on what might cause this (mis-)behaviour would be greatly

TIA & cheers

we have just discovered that the problem only occurs in development
mode -- production mode works fine. so it seems certain constants
(modules/classes) "get lost" by rails' (re-)loading behaviour in
development mode. but how to overcome this issue? anyone care to
help us out on this one?


jens wille [04/01/07 17:49]:

Hi Jen - have you tried Image::ActiveRecord::Base?


hi paul!

Paul Corcoran [05.01.2007 18:35]:

Hi Jen - have you tried Image::ActiveRecord::Base?

well, no... since the class in question is actually Image::Base, not
Image::ActiveRecord::Base. it's only Image that /inherits/ from
ActiveRecord::Base, via Entity. so i assume the path is totally
correct - as running in production mode shows -, it's just that this
class (Image::Base) seems to be "there" only immediately after
starting the mongrel servers (i.e., after loading the whole code
base) -- which appears quite strange to me.

i mean, why do i even have to require image/base in environment.rb
in order to get that constant? i don't usually have to for other
classes and modules in lib/ (of course, there are other reasons why
i might have to do that, which apply to image/base, too). it seems
that this issue has in some way to do with the fact that Image
already is a model class. but in what way? and what can we do about
it? i'm kinda stuck...


I will jump in here, with more of a question than an answer. I am
totally new to rails. Have developed an app locally on my laptop. It
works fine. I just tried to set up my web host environment for the
first time and it seems that ALL of my application classes are not
being found when I run in production environment. Furthermore, in the
"script/console production" , when I try to run Rails:Info I get
NameError: uninitialized constant Rails::Info. Any quick tips for a

nevermind :slight_smile: Pilot error copying files onto web host was my problem.