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 Garcia wrote:> 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 > > arnaudArnaud- 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 Zygmuntowicz a ?crit :> > On Jun 18, 2006, at 9:18 AM, Arnaud Garcia wrote: > >> 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 > > > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > 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 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 Garcia <arnaud.garcia@sim.hcuge.ch> wrote:> Ezra Zygmuntowicz a ?crit : > > > > > On Jun 18, 2006, at 9:18 AM, Arnaud Garcia wrote: > > > >> 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 > > > > > > 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 > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > 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 > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Derek Haynes HighGroove Studios - http://www.highgroove.com San Mateo, CA | Atlanta, GA Keeping it Simple. 650.276.0908
Arnaud Garcia 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