Hi, I have model called Player, it looks something like this: class Player < ActiveRecord::Base @@image_path = '''' def self.image_path=(path) @@image_path = path end def path_to_images @@image_path + id.to_s + "/" end end In environment.rb I want to set the path where images are stored: Player.image_path = "/images/players/" This works on the first request, but not on any subsequent requests. @@image_path is reset to '''' after the first request. It works when I reboot WEBrick, but again in the 2nd request it stops working. Any way to overcome this? Jeroen
Hi, I have model called Player, it looks something like this: class Player < ActiveRecord::Base @@image_path = '''' def self.image_path=(path) @@image_path = path end def path_to_images @@image_path + id.to_s + "/" end end In environment.rb I want to set the path where images are stored: Player.image_path = "/images/players/" This works on the first request, but not on any subsequent requests. @@image_path is reset to '''' after the first request. It works when I reboot WEBrick, but again in the 2nd request it stops working. Any way to overcome this? Jeroen
Jeroen, This is a consequence of the "reload" feature in development mode. The environment.rb is read only once (as is the Rails framework itself), but the classes are reloaded with every request, resulting in the behavior you described. One solution I''ve used is to have to have a separate "configuration" module for the persistent configuration: class Player < ActiveRecord::Base module Configuration @@image_path = '''' mattr_accessor :image_path end def path_to_images "#{Configuration.image_path}#{id.to_s}/" end end ... Player::Configuration.image_path = "/blah/blah/blah" Another option is to set the image path as a constant in your environment.rb: IMAGE_PATH = "/blah/blah/blah" But I find this less attractive than the module-based approach, especially when there are multiple configuration options per class. - Jamis On Oct 21, 2005, at 6:53 AM, Jeroen Houben wrote:> Hi, > > I have model called Player, it looks something like this: > > class Player < ActiveRecord::Base > > @@image_path = '''' > > def self.image_path=(path) > @@image_path = path > end > > > def path_to_images > @@image_path + id.to_s + "/" > end > > end > > In environment.rb I want to set the path where images are stored: > > Player.image_path = "/images/players/" > > This works on the first request, but not on any subsequent > requests. @@image_path is reset to '''' after the first request. It > works when I reboot WEBrick, but again in the 2nd request it stops > working. > > Any way to overcome this? > > Jeroen > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jamis Buck wrote:> Jeroen, > > This is a consequence of the "reload" feature in development mode. > The environment.rb is read only once (as is the Rails framework > itself), but the classes are reloaded with every request, resulting > in the behavior you described. >Thanks James. So in production mode classes are also read just once? That''s pretty sweet (I''m usually developing in PHP5 where classes everything is read on each request, unless you use some sort of bytecode caching module).> One solution I''ve used is to have to have a separate "configuration" > module for the persistent configuration: > > class Player < ActiveRecord::Base > module Configuration > @@image_path = '''' > mattr_accessor :image_path > end > > def path_to_images > "#{Configuration.image_path}#{id.to_s}/" > end > end > > ... > > Player::Configuration.image_path = "/blah/blah/blah" > > Another option is to set the image path as a constant in your > environment.rb: > > IMAGE_PATH = "/blah/blah/blah" > > But I find this less attractive than the module-based approach, > especially when there are multiple configuration options per class.I agree. So suppose another one of my Active record classes also needs an image_path configuration, would doing exactly the same for say a Match class cause a clash? Or is the Configuration Module a seperate entity in each class? Disclaimer: I''m just getting to know Ruby ;-) Jeroen
On Oct 21, 2005, at 12:03 PM, Jeroen Houben wrote:> Jamis Buck wrote: > >> One solution I''ve used is to have to have a separate >> "configuration" module for the persistent configuration: >> >> class Player < ActiveRecord::Base >> module Configuration >> @@image_path = '''' >> mattr_accessor :image_path >> end >> >> def path_to_images >> "#{Configuration.image_path}#{id.to_s}/" >> end >> end >> >> ... >> >> Player::Configuration.image_path = "/blah/blah/blah" >> >> Another option is to set the image path as a constant in your >> environment.rb: >> >> IMAGE_PATH = "/blah/blah/blah" >> >> But I find this less attractive than the module-based approach, >> especially when there are multiple configuration options per class. >> > > I agree. > So suppose another one of my Active record classes also needs an > image_path configuration, would doing exactly the same for say a > Match class cause a clash? Or is the Configuration Module a > seperate entity in each class? Disclaimer: I''m just getting to know > Ruby ;-)The containing class (or module) acts as a namespace, so you can have identically named modules as long as they exist in separate classes/ modules, i.e.: class Player < ActiveRecord::Base module Configuration ... end end class Match < ActiveRecord::Base module Configuration ... end end Both Configuration modules are distinct. You would access them as Player::Configuration and Match::Configuration, respectively. - Jamis