Constants already initialized warnings

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?

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

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.

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 [email protected] 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.

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

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

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