# Environment variables unknown in Model with Namespace

**URL:** <https://www.ruby-forum.com/t/environment-variables-unknown-in-model-with-namespace/120557>\
**Category:** Rails\
**Created:** [November 18, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/environment-variables-unknown-in-model-with-namespace/120557 "2007-11-18T15:01:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bas\_van\_Westing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@Bas\_van\_Westing](https://www.ruby-forum.com/u/Bas_van_Westing)\
**Post date:** [November 18, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/environment-variables-unknown-in-model-with-namespace/120557/1 "2007-11-18T15:01:25Z")

</div>

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  
=\> :❌: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

---

<div class="post-metadata">

**Author:** ![Bas\_van\_Westing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@Bas\_van\_Westing](https://www.ruby-forum.com/u/Bas_van_Westing)\
**Post date:** [November 18, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/environment-variables-unknown-in-model-with-namespace/120557/2 "2007-11-18T16:26:56Z")

</div>

On Nov 18, 2007, at 3:00 PM, Bas van Westing wrote:

> constant Y::MyModel::X"  
> "vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/
> 
> 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.

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

---

<div class="post-metadata">

**Author:** ![Bas\_van\_Westing](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@Bas\_van\_Westing](https://www.ruby-forum.com/u/Bas_van_Westing)\
**Post date:** [November 20, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/environment-variables-unknown-in-model-with-namespace/120557/3 "2007-11-20T21:42:42Z")

</div>

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
