Hi everyone, If I save some object in session and then restart the server, I get the following error unless I restart my web browser (or delete session files): Session contains objects whose class definition isn''t available. Remember to require the classes for all objects kept in the session. (Original exception: uninitialized constant TZInfo [NameError]) I know what the problem is. Rails hasn''t loaded "TZInfo::Timezone" class definition yet so it doesn''t know how to handle "TZInfo::Timezone" object stored in the session. If an ActiveRecord model is saved in the session, using "model" method in "ApplicationController" with the symbol for the model name does the trick of loading the class definition before the session data is accessed. But what about non-ActiveRecord objects? How do I force loading the class definition? Also, I read somewhere that we don''t need to use "model" method any more for loading ActiveRecord models. Can anyone confirm this? I will appreciate any comments, thanks! best, daesan PS: I have already looked at the rails wiki pages. The solution to this particular problem isn''t covered there.
Dae San Hwang wrote:> If I save some object in session and then restart the server, I get the > following error unless I restart my web browser (or delete session files): > > Session contains objects whose class definition isn''t available. > Remember to require the classes for all objects kept in the session. > (Original exception: uninitialized constant TZInfo [NameError]) > > I know what the problem is. Rails hasn''t loaded "TZInfo::Timezone" > class definition yet so it doesn''t know how to handle "TZInfo::Timezone" > object stored in the session. If an ActiveRecord model is saved in the > session, using "model" method in "ApplicationController" with the symbol > for the model name does the trick of loading the class definition before > the session data is accessed. But what about non-ActiveRecord objects? > How do I force loading the class definition?You should be able to get TZInfo to load before the session loads by adding require_gem ''tzinfo'' to your environment.rb. I don''t think this will solve the problem with TZInfo though. TZInfo uses a class for each defined Timezone (all of these subclass TZInfo::Timezone). When you use Timezone.get, the class for the Timezone is loaded and a singleton instance of the class is returned. For example, the class TZInfo::Definitions::Europe::London defines the timezone Europe/London. When you put a Timezone in the session, a reference to its definition class will be stored. On reloading after a restart, Rails won''t understand how to load these classes using its normal rules for handling requires. Unless the specific timezone has already been loaded, the restoration of the session will always fail. You''re probably best off storing the Timezone identifier and reloading with Timezone.get: session[:tz] = timezone.identifier timezone = TZInfo::Timezone.get(session[:tz]) I''ll see if I can do something about this in a future TZInfo release. Phil -- Philip Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby
Thank you Phil for the reply! I''ve already had "require" statement in environment.rb and as you said this doesn''t solve the problem. It will be super cool if you can do something about it in TZInfo! For now, I will just keep erasing session files whenever I restart the server. Thanks again, daesan On Apr 25, 2006, at 5:40 AM, Philip Ross wrote:> Dae San Hwang wrote: >> If I save some object in session and then restart the server, I >> get the following error unless I restart my web browser (or delete >> session files): >> Session contains objects whose class definition isn''t available. >> Remember to require the classes for all objects kept in the session. >> (Original exception: uninitialized constant TZInfo [NameError]) >> I know what the problem is. Rails hasn''t loaded >> "TZInfo::Timezone" class definition yet so it doesn''t know how to >> handle "TZInfo::Timezone" object stored in the session. If an >> ActiveRecord model is saved in the session, using "model" method >> in "ApplicationController" with the symbol for the model name does >> the trick of loading the class definition before the session data >> is accessed. But what about non-ActiveRecord objects? How do I >> force loading the class definition? > > You should be able to get TZInfo to load before the session loads > by adding require_gem ''tzinfo'' to your environment.rb. > > I don''t think this will solve the problem with TZInfo though. > TZInfo uses a class for each defined Timezone (all of these > subclass TZInfo::Timezone). When you use Timezone.get, the class > for the Timezone is loaded and a singleton instance of the class is > returned. For example, the class > TZInfo::Definitions::Europe::London defines the timezone Europe/ > London. > > When you put a Timezone in the session, a reference to its > definition class will be stored. On reloading after a restart, > Rails won''t understand how to load these classes using its normal > rules for handling requires. Unless the specific timezone has > already been loaded, the restoration of the session will always fail. > > You''re probably best off storing the Timezone identifier and > reloading with Timezone.get: > > session[:tz] = timezone.identifier > timezone = TZInfo::Timezone.get(session[:tz]) > > I''ll see if I can do something about this in a future TZInfo release. > > Phil > > -- > Philip Ross > http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsDae San Hwang daesan@gmail.com