Share an hash

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

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

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 ?

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

basically, this is the code

Arnaud-
[email protected]
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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


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

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