# How to properly define constants in helpers?

**URL:** <https://www.ruby-forum.com/t/how-to-properly-define-constants-in-helpers/194112>\
**Category:** Rails\
**Created:** [August 30, 2010, 4:46pm UTC](https://www.ruby-forum.com/t/how-to-properly-define-constants-in-helpers/194112 "2010-08-30T16:46:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [August 30, 2010, 4:46pm UTC](https://www.ruby-forum.com/t/how-to-properly-define-constants-in-helpers/194112/1 "2010-08-30T16:46:00Z")

</div>

Let’s say I have a helper module like this

module ThingsHelper  
DEFAULT\_OPTIONS = { … }.freeze

def my\_helper(options = {})  
options.reverse\_merge!(DEFAULT\_OPTIONS)  
…  
end  
end

Then, in development environment, I get

…/app/helpers/things\_helper.rb:2: warning: already initialized  
constant DEFAULT\_OPTIONS

Is there anything I can do about this warning?

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [August 30, 2010, 5:17pm UTC](https://www.ruby-forum.com/t/how-to-properly-define-constants-in-helpers/194112/2 "2010-08-30T17:17:38Z")

</div>

I think you are having the same problem as I at  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/b2ff5ff2b9e009d7](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/b2ff5ff2b9e009d7)  
. I bet your helper is getting included twice.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [August 30, 2010, 6:41pm UTC](https://www.ruby-forum.com/t/how-to-properly-define-constants-in-helpers/194112/3 "2010-08-30T18:41:22Z")

</div>

On Monday 30 August 2010, richardsugg wrote:

> I think you are having the same problem as I at  
> [http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/)  
> b2ff5ff2b9e009d7 . I bet your helper is getting included twice.

I’m not sure. I’m _never_ including it explicitly. As it is a helper, it  
is loaded automatically.

However, there is one special thing. I’m defining the helper in question  
in an engine and to have it included automatically by helper :all, I add  
the path engine helper path in an initializer block of the engine

initializer ‘make MyEngine helpers automatically loadable’,  
:before =\> “action\_controller.set\_configs” do |app|  
paths.app.helpers.each do |path|  
app.paths.app.helpers \<\< path  
end  
end

and unfortunately, that in some way appears to cause the warning.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
