# Constants already initialized warnings

**URL:** <https://www.ruby-forum.com/t/constants-already-initialized-warnings/125608>\
**Category:** Rails\
**Created:** [January 10, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/constants-already-initialized-warnings/125608 "2008-01-10T16:19:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [January 10, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/constants-already-initialized-warnings/125608/1 "2008-01-10T16:19:21Z")

</div>

I have some constants defined in my application\_helper.rb and I keep  
getting these warning: already initialzed constant \_\_\_\_\_\_\_\_

I greped my project and made sure these constants are not defined or  
set elsewhere. is my application\_helper being loaded multiple times?

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [January 10, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/constants-already-initialized-warnings/125608/2 "2008-01-10T16:36:20Z")

</div>

You should check if it’s defined using defined?.

For example, you’ll see something like this in your environment.rb:

RAILS\_GEM\_VERSION = ‘1.2.6’ unless defined? RAILS\_GEM\_VERSION

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [January 11, 2008, 12:22am UTC](https://www.ruby-forum.com/t/constants-already-initialized-warnings/125608/3 "2008-01-11T00:22:48Z")

</div>

thanks, that got rid of the warnings.

still it suggests my application\_helper.rb is being loaded multiple  
times per request. is that normal? doesn’t seem efficient.

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [January 11, 2008, 12:26am UTC](https://www.ruby-forum.com/t/constants-already-initialized-warnings/125608/4 "2008-01-11T00:26:54Z")

</div>

Not multiple times per request, but rather reloaded per request. The  
first  
request may be fine, but subsequent requests will show that error. A  
constant is exactly that: a constant variable for the life of your  
application. If you’re defining constants in your application\_helper,  
maybe  
you should be doing it somewhere else?

On Jan 11, 2008 9:52 AM, rubynuby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thanks, that got rid of the warnings.
> 
> still it suggests my application\_helper.rb is being loaded multiple  
> times per request. is that normal? doesn’t seem efficient.
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [January 11, 2008, 12:40am UTC](https://www.ruby-forum.com/t/constants-already-initialized-warnings/125608/5 "2008-01-11T00:40:55Z")

</div>

On Jan 11, 2008, at 12:22 AM, rubynuby wrote:

> still it suggests my application\_helper.rb is being loaded multiple  
> times per request. is that normal? doesn’t seem efficient.

No, no, you shouldn’t be throwing any defined? like that. One uses  
defined? when it makes sense in her context, not to hide a suspicious  
behaviour.

What you are seeing is something that shouldn’t happen, so the right  
thing to do is to discover why it is happening and fix it.

I think a trick to debug that could be to throw

```
 RAILS_DEFAULT_LOGGER.debug(caller.join("\n")) if defined?

```

SOME\_CONSTANT  
SOME\_CONSTANT = value

in the helper module. There we use defined? to print the execution  
stack just in the second run.

– fxn

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [January 11, 2008, 1:59am UTC](https://www.ruby-forum.com/t/constants-already-initialized-warnings/125608/6 "2008-01-11T01:59:25Z")

</div>

On Jan 11, 2008, at 12:26 AM, Ryan B. wrote:

> Not multiple times per request, but rather reloaded per request. The  
> first request may be fine, but subsequent requests will show that  
> error. A constant is exactly that: a constant variable for the life  
> of your application. If you’re defining constants in your  
> application\_helper, maybe you should be doing it somewhere else?

But this is not the case with application\_helper.rb, in the sense that  
ApplicationHelper is autoloaded:

```
 $ script/console
 Loading development environment (Rails 2.0.2)
 >> Dependencies.autoloaded? ApplicationHelper
 => true

```

That is a side-effect of prepare\_application.

So, you know, you get a fresh new module in the fresh new constant  
ApplicationHelper per request in development mode. Any nested constant  
is new in consequence, hence if everything is set up as usual you  
don’t get those warnings.

– fxn
