environment variables unknown in Model with Namespace

Hi,

I have a problem with a model which is defined within a namespace. It does not seem to find another namespace that was defined in the environment.

#config/initializers.rb module X   class Exception < Exception     ...some custom exception definitions here...   end end

#app/models/y/my_model.rb class Y::MyModel < ActiveRecord::Base   validates_presence_of :attr, :message => X::Exception.new('init).to_string_reference end

This setup fails during boot because the model searches for Y::MyModel::X, which it cannot find: "vendor/rails/activerecord/lib/../../activesupport/lib/active_support/ dependencies.rb:478:in `const_missing':NameError: uninitialized constant Y::MyModel::X"

When I try the following setup:

#app/models/y/my_model.rb class Y::MyModel < ActiveRecord::Base   validates_presence_of :attr, :message => ::x::Exception.new('init).to_string_reference end

It still fails during boot because it still cannot find X: "vendor/rails/activerecord/lib/../../activesupport/lib/active_support/ dependencies.rb:266:in `load_missing_constant':NameError: uninitialized constant X"

However, in my normal models (without a namespace) it does work:

#app/models/my_other_model.rb class MyOtherModel < ActiveRecord::Base   validates_presence_of :attr, :message => X::Exception.new('init).to_string_reference end

This loads fine. I don't understand why it does not work in the namespace situation. Does anybody know what I'm doing wrong? I'm using Rails 2.0RC1 by the way.

Thanks in advance, Bas van Westing

To give a brief explanation constant autoloading assumes two things about a missing constant with qualified name "Foo::Bar":

   1. The definition lives in a file whose filename ends       with "Foo::Bar".underscore + '.rb'

   2. The resulting path "foo/bar.rb" exists under one of the       directories in Dependencies.load_path

When Rails boots Dependencies.load_path is initialized with an array of standard directories (like RAILS_ROOT/app/models and friends), but since "X" is defined in config/initializers.rb it won't be autoloaded (that file is non-standard AFAIK, are you sure that's its name?).

-- fxn

Hi Xavier,

The config/initializer.rb was a typo. It is in config/initializers/ error_codes.rb The config/initializers/* files are loaded on boot by default with rails 2.0RC1.

The problem is still unclear to me because the constant is available in the other model (without the module/namespace). Both models are loaded in the same boot, so that cannot be the problem. Do you have any more ideas?

Thanks, Bas