# Where to put a large constant?

**URL:** <https://www.ruby-forum.com/t/where-to-put-a-large-constant/73387>\
**Category:** Rails\
**Created:** [October 24, 2006, 9:58am UTC](https://www.ruby-forum.com/t/where-to-put-a-large-constant/73387 "2006-10-24T09:58:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [October 24, 2006, 9:58am UTC](https://www.ruby-forum.com/t/where-to-put-a-large-constant/73387/1 "2006-10-24T09:58:21Z")

</div>

In a registration form I allow the user to select several spoken  
languages and of course to minimize tomfoolery I am using selects. In  
the view I have:

\<%= select(‘person’, ‘language1’, LANGUAGES, options = {}) %\>

In my people\_helper.rb I have:

module PeopleHelper

# constant for language selection in ‘\_form\_background.rhtml’

LANGUAGES = %w{Akan  
Algerian  
Amharic  
Arabic  
…  
Xhosa  
Yoruba  
Zulu }

end

However rails still complains, “uninitialized constant LANGUAGES.” I  
can put it above my person.rb model but that is messy. Is there a  
better way?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [October 24, 2006, 10:02am UTC](https://www.ruby-forum.com/t/where-to-put-a-large-constant/73387/2 "2006-10-24T10:02:58Z")

</div>

from what i know, and from what works for me, i put it in enviornment.rb  
file.  
as i understand, all of ruby’s constants are loaded when the server is  
first started, so enviornment.rb is the given place to put it.  
whether the theory, or logical structure is correct, i don’t know - -  
but it works great. (after restarting the server, obviously).

hth,

harp

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [October 24, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/where-to-put-a-large-constant/73387/3 "2006-10-24T18:16:18Z")

</div>

Worked perfectly. Thanks!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [October 24, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/where-to-put-a-large-constant/73387/4 "2006-10-24T22:12:21Z")

</div>

That is a great solution. It definately makes sense to only initialize  
the array when it is needed and not globally. Every time you learn  
something, you realize there is always a better way right after you  
finish implementing the solution!!! Thanks to everyone who replied!!!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [October 24, 2006, 6:35pm UTC](https://www.ruby-forum.com/t/where-to-put-a-large-constant/73387/5 "2006-10-24T18:35:32Z")

</div>

That solution is still a little on the messy side (in terms of  
cluttering up environment.rb). In addition, you have the (admittedly  
very minor) overhead of initializing the array before any  
controller/action called, even though only the controller dealing with  
the Person model actually uses it.

What I would do would be to create a languages.rb file in your lib  
directory with nothing in it except the constant definition. Then call  
“require ‘languages’” at the top of the appropriate controller. In fact,  
if it is only used in one action, you can even call it just for that  
specific action:

class PeopleController \< ApplicationController

…

def register  
require ‘languages’  
# whatever else your action needs  
@person = Person.new

```
...

```

end

…

end

Just a different (and cleaner, in my opinion) way to do it. Hope this  
helps!
