# Share an hash

**URL:** <https://www.ruby-forum.com/t/share-an-hash/95895>\
**Category:** Rails\
**Created:** [June 18, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/share-an-hash/95895 "2006-06-18T18:21:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arnaud\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@Arnaud\_G](https://www.ruby-forum.com/u/Arnaud_G)\
**Post date:** [June 18, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/share-an-hash/95895/1 "2006-06-18T18:21:46Z")

</div>

Hello,

How to share an Hash between all objects (model, controller ect …)  
=\> I did a static Hash in my model where a static method fill it

basically, this is the code

class Person \< ActiveModel  
@@anHash.new

def Person.anHelperMethod(aKey)  
if anHash.size==0  
… load the hash, some SQL to fill the Hash  
end  
return anHash[aKey]  
end

=\> The problem,: it always reload the Hash ! as it was a local variable  
… I don’t understand since it has the good behavior in the console  
env …

thanks for help

arnaud

---

<div class="post-metadata">

**Author:** ![Arnaud\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@Arnaud\_G](https://www.ruby-forum.com/u/Arnaud_G)\
**Post date:** [June 18, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/share-an-hash/95895/2 "2006-06-18T21:48:37Z")

</div>

On Jun 18, 2006, at 9:18 AM, Arnaud G. wrote:

> def Person.anHelperMethod(aKey)
> 
> thanks for help
> 
> arnaud

Arnaud-

```
If you want to share a hash between all of your app then you should

```

put it in environment.rb like so:

MYHASH = {:foo =\> ‘bar’, :baz =\> 'qux}.freeze

-Ezra

---

<div class="post-metadata">

**Author:** ![Arnaud\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@Arnaud\_G](https://www.ruby-forum.com/u/Arnaud_G)\
**Post date:** [June 18, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/share-an-hash/95895/3 "2006-06-18T22:26:03Z")

</div>

Ezra Z. a écrit :

> > class Person \< ActiveModel  
> > variable … I don’t understand since it has the good behavior in  
> > If you want to share a hash between all of your app then you

thanks for the tip, but do you know why static variable inside a  
ActiveModel are not available … it seems they are always reset ?

---

<div class="post-metadata">

**Author:** ![Arnaud\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@Arnaud\_G](https://www.ruby-forum.com/u/Arnaud_G)\
**Post date:** [June 19, 2006, 1:17am UTC](https://www.ruby-forum.com/t/share-an-hash/95895/4 "2006-06-19T01:17:19Z")

</div>

Rails models don’t maintain state btw http requests. As Ezra said, put  
data that needs to be shared across the app in a constant.

- Derek

On 6/18/06, Arnaud G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > basically, this is the code
> > 
> > Arnaud-  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> thanks for the tip, but do you know why static variable inside a  
> ActiveModel are not available … it seems they are always reset ?
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Derek H.  
HighGroove Studios - [http://www.highgroove.com](http://www.highgroove.com)  
San Mateo, CA | Atlanta, GA  
Keeping it Simple.  
650.276.0908

---

<div class="post-metadata">

**Author:** ![Arnaud\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@Arnaud\_G](https://www.ruby-forum.com/u/Arnaud_G)\
**Post date:** [June 19, 2006, 8:31am UTC](https://www.ruby-forum.com/t/share-an-hash/95895/5 "2006-06-19T08:31:55Z")

</div>

Arnaud G. wrote:

> do you know why static variable inside a  
> ActiveModel are not available … it seems they are always reset ?

In development mode your ActiveRecord model classes are reloaded on each  
request. In production mode they are not.

regards

Justin
