This has troubled me for a while, and I haven''t found the right solution yet for it. Say I have six different models that need to pull localized data from the db (product descriptions, for example). Each model has a number of methods. My users have, in their sessions, a language setting. To avoid repeating myself, I''d like those models to become aware of the user''s language setting without having to explicity pass it to every method/model. This seems to break an MVC paradigm. I''m told that cattr_accessor is not useful here because it uses a class variable which are kept in memory by the ruby FastCGI process. Is there an alternative? Joshua Sierles _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
You could try adding a before filter to your application controller that would dig out the language setting from the session and then set the class variable on the active record model. You would probably also want to reset it to a default language if there were no logged in user. This way, even though the class variable persists between requests, it always has the right value when you need to use it. On Jul 19, 2005, at 5:34 PM, Joshua Sierles wrote:> This has troubled me for a while, and I haven''t found the right > solution yet for it. > > Say I have six different models that need to pull localized data > from the db (product descriptions, for example). Each model has a > number of methods. My users have, in their sessions, a language > setting. > > To avoid repeating myself, I''d like those models to become aware of > the user''s language setting without having to explicity pass it to > every method/model. This seems to break an MVC paradigm. > > I''m told that cattr_accessor is not useful here because it uses a > class variable which are kept in memory by the ruby FastCGI process. > > Is there an alternative? > > Joshua Sierles > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 7/20/05, Joshua Sierles <jsierles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This has troubled me for a while, and I haven''t found the right solution yet > for it. > > Say I have six different models that need to pull localized data from the db > (product descriptions, for example). Each model has a number of methods. My > users have, in their sessions, a language setting. > > To avoid repeating myself, I''d like those models to become aware of the > user''s language setting without having to explicity pass it to every > method/model. This seems to break an MVC paradigm. > > I''m told that cattr_accessor is not useful here because it uses a class > variable which are kept in memory by the ruby FastCGI process.You''re told wrong. It''s kept in memory, but as only one user is accessing a given fastcgi process at a time, you can still use it. Just be sure to set it every time in your before_filter. I wrote a blog post about this a few days back. http://www.koziarski.net/archives/2005/07/16/environment> Is there an alternative? > > Joshua Sierles > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Cheers Koz