I tried to keep track of the number of access to a specific controller. I declared ''@@mycount = 0'' in the controller class. The methods in the controller will increment the @@mycount (@@mycount = @@mycount + 1) and print the value to the html page and show in the browser. The value is always 1, no matter how many times I access the controller and its action(s). Is this not the right way to create and share a global variable in memory? -- Posted via http://www.ruby-forum.com/.
I moved the global variables to a new class defined inside environment.rb and get around the issue. However, I am still curious why the global variables can''t be defined in controller class (and I suspect it won''t work in model class as well). rw y. wrote:> I tried to keep track of the number of access to a specific controller. > I declared ''@@mycount = 0'' in the controller class. The methods in the > controller will increment the @@mycount (@@mycount = @@mycount + 1) and > print the value to the html page and show in the browser. The value is > always 1, no matter how many times I access the controller and its > action(s). Is this not the right way to create and share a global > variable in memory?-- Posted via http://www.ruby-forum.com/.
I''ll bet that it''s because, when Rails is running in development mode, the controller class gets reloaded on each request, thereby overwriting the value of any class variables. On 04/04/2006, at 12:03 PM, rw y. wrote:> I moved the global variables to a new class defined inside > environment.rb and get around the issue. However, I am still curious > why the global variables can''t be defined in controller class (and I > suspect it won''t work in model class as well). > > rw y. wrote: >> I tried to keep track of the number of access to a specific >> controller. >> I declared ''@@mycount = 0'' in the controller class. The methods >> in the >> controller will increment the @@mycount (@@mycount = @@mycount + >> 1) and >> print the value to the html page and show in the browser. The >> value is >> always 1, no matter how many times I access the controller and its >> action(s). Is this not the right way to create and share a global >> variable in memory? > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On 4/04/2006, at 2:03 PM, rw y. wrote:> I moved the global variables to a new class defined inside > environment.rb and get around the issue. However, I am still curious > why the global variables can''t be defined in controller class (and I > suspect it won''t work in model class as well).Controllers and Models don''t persist, they''re killed off at the end of each request. Models are stateful, the rest of Rails is stateless, excepting sessions. If you want to store data use a session or a model. -- Phillip Hutchings phillip.hutchings@sitharus.com http://www.sitharus.com/
Phillip Hutchings wrote:> On 4/04/2006, at 2:03 PM, rw y. wrote: > >> I moved the global variables to a new class defined inside >> environment.rb and get around the issue. However, I am still curious >> why the global variables can''t be defined in controller class (and I >> suspect it won''t work in model class as well). > > Controllers and Models don''t persist, they''re killed off at the end > of each request. Models are stateful, the rest of Rails is stateless, > excepting sessions. If you want to store data use a session or a model. > > -- > Phillip Hutchings > phillip.hutchings@sitharus.com > http://www.sitharus.com/The session is indeed the way to go - and is easy: session[:mycount] += 1 would increment the count, and you can retrieve it from any controller or view. hth, Keith -- Posted via http://www.ruby-forum.com/.
Keith Lancaster wrote:> Phillip Hutchings wrote: >> On 4/04/2006, at 2:03 PM, rw y. wrote: >> >>> I moved the global variables to a new class defined inside >>> environment.rb and get around the issue. However, I am still curious >>> why the global variables can''t be defined in controller class (and I >>> suspect it won''t work in model class as well). >> >> Controllers and Models don''t persist, they''re killed off at the end >> of each request. Models are stateful, the rest of Rails is stateless, >> excepting sessions. If you want to store data use a session or a model. >> >> -- >> Phillip Hutchings >> phillip.hutchings@sitharus.com >> http://www.sitharus.com/ > > The session is indeed the way to go - and is easy: > > session[:mycount] += 1 > > would increment the count, and you can retrieve it from any controller > or view. > > hth, > KeithThanks. But, wouldn''t session scope to only a specific client, not the entire application space? -- Posted via http://www.ruby-forum.com/.
On 4/04/2006, at 5:26 PM, rw y. wrote:> > Thanks. But, wouldn''t session scope to only a specific client, not > the > entire application space?There isn''t an application space. In some situations it may appear that there is one, but don''t count on it. Running multiple instances or clustered serving would all mess it up. Use the database, that''s what it''s for. -- Phillip Hutchings phillip.hutchings@sitharus.com http://www.sitharus.com/ -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2234 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/97f46b49/smime.bin
On 4-apr-2006, at 1:52, rw y. wrote:> I tried to keep track of the number of access to a specific > controller. > I declared ''@@mycount = 0'' in the controller class. The methods in > the > controller will increment the @@mycount (@@mycount = @@mycount + 1) > and > print the value to the html page and show in the browser. The > value is > always 1, no matter how many times I access the controller and its > action(s). Is this not the right way to create and share a global > variable in memory?Global variables are prefixed with $. $counter += 1; Note that counters are NOT shared between FCGI instances, so essentially under any heavy use your global variables will be unsynchronized across application instances. As to your problem I think your controller class gets reloaded on every request, so it''s class vars are getting reloaded. Note that what youa re trying to achieve is bad practice. -- Julian ''Julik'' Tarkhanov me at julik.nl