Forum: Ruby on Rails how to save module variable on every requestrails

Posted by aldm alert (aldm)
on 2013-01-24 22:44
Hi,

I have module that imports data from json file to hash. Json file is
pretty big so I don't want loading to take place on every request
because it takes some time. It would be best to do this just on server
start. I have a class variable in module called "data" which I'd like to
set only on server start and then I can access to it on every request
with SomeModule.data (I use mattr_accessor). I tried to import data in
application config.after_initlize method but id doesnt work - data
persists only on first request and on second request data is nil.

Is there any way to do this beside session, and can you give little
explanation on this. I know I can save this data in session but I'd like
to do this other way.

Thanks in advance
Posted by Robert Walker (robert4723)
on 2013-01-25 07:34
aldm alert wrote in post #1093626:
> I have module that imports data from json file to hash. Json file is
> pretty big so I don't want loading to take place on every request
> because it takes some time. It would be best to do this just on server
> start. I have a class variable in module called "data" which I'd like to
> set only on server start and then I can access to it on every request
> with SomeModule.data (I use mattr_accessor). I tried to import data in
> application config.after_initlize method but id doesnt work - data
> persists only on first request and on second request data is nil.
>
> Is there any way to do this beside session, and can you give little
> explanation on this. I know I can save this data in session but I'd like
> to do this other way.

Use of class variables, especially in Ruby, are generally considered a 
bad idea.

Read this to understand why and for some possible solutions:
http://www.oreillynet.com/pub/a/ruby/excerpts/ruby...
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.