# Module + Constant Issue

**URL:** <https://www.ruby-forum.com/t/module-constant-issue/116290>\
**Category:** Rails\
**Created:** [October 4, 2007, 5:53am UTC](https://www.ruby-forum.com/t/module-constant-issue/116290 "2007-10-04T05:53:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 4, 2007, 5:53am UTC](https://www.ruby-forum.com/t/module-constant-issue/116290/1 "2007-10-04T05:53:36Z")

</div>

I ran into the following problem in my application when I went from my  
dev environment to my staging environment. Modules and classes that  
were previously autoloaded just fine in dev ended up with a strange  
error in staging:

NameError: uninitialized constant Rails::Initializer::…

Weird.

Well, I managed to reproduce it, and here is the procedure. I’m  
wondering if the approach I’m using to setup environment-specific  
values for these constants is the culprit, and maybe there’s a better  
way. Or maybe this is some sort of wacky bug? Anyway, here’s the  
procedure. Let me know what you think.

—\>8— cut here —\>8—

1. Create a new Rails app, e. g. $ rails constant

2. Edit the environment.rb file to add your own module with  
configuration constant default values with the idea you could  
“override” these by specifying them in the specific environment file:

At the end of config/environment.rb, add:

module AppConstants  
IMPORTANT\_VALUE = “x” unless defined?(AppConstants::IMPORTANT\_VALUE)  
end

1. Create a Ruby module in the file lib/app\_constants/list.rb, noting  
the “top level” name of the module matches the one specified in the  
environment file:

module AppConstants  
module List  
LIST\_CONSTANT = “list\_constant”  
end  
end

1. Fire up the console and probe the constant values:

Loading development environment.

> > AppConstants::List::LIST\_CONSTANT  
> > =\> “list\_constant”
> 
> > AppConstants::IMPORTANT\_VALUE  
> > =\> “x”

1. Quit from the console and add development-specific value for the  
constant defined in the environment file:

At the end of config/environments/development.rb, add:

module AppConstants  
IMPORTANT\_VALUE = “definitely\_not\_x”  
end

1. Fire up the console again and probe the constant values:

Loading development environment.

> > AppConstants::List::LIST\_CONSTANT  
> > NameError: uninitialized constant  
> > Rails::Initializer::AppConstants::List  
> > from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/  
> > active\_support/dependencies.rb:263:in `load_missing_constant' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/ active_support/dependencies.rb:452:in`const\_missing’  
> > from (irb):1
> 
> > AppConstants::IMPORTANT\_VALUE  
> > =\> “definitely\_not\_x”
> 
> > AppConstants::List::LIST\_CONSTANT  
> > NameError: uninitialized constant  
> > Rails::Initializer::AppConstants::List  
> > from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/  
> > active\_support/dependencies.rb:263:in `load_missing_constant' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/ active_support/dependencies.rb:452:in`const\_missing’  
> > from (irb):3
