Ruby class variable is reset after rails app initialized -


I tried to assign a class static variable like this

  class Querylogger & lt ; Logger @@ query_logger_default_instance = nil def self.default_instance # Use global variable because stable variable does not work @@ query_logger_default_instance || = Self.new (STDOUT) End End  

My Rails app in the initializers folder, I have added a file with this code block

  ActiveRecord: : Base.logger = Querylogger.default_instance  

In a request (the operation of the controller), I call it: Querylogger.default_instance .

My impression is that call to default_instance will always report the same though, it is not.

Now I try to see the stuff in NetBeans by setting the breakpoint in net_bans. In the event of the expected, the default_ is called twice in the interval, one due to the initial block due to the call for another action. Surprising thing is in both times, inside the Netbeans Inspector @@ query_logger_default_instance Report Zero The first Nile report is correct, but the other surprised me. It looks like a static variable, Rail application gets reset after starting. Is there some magic?

It depends if you are running in the development environment, per request basis, its The reason is that you can make changes for them and view the server without restarting.

So I think your class variable will be reset for each request.

Try to walk in the production.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -