ruby on rails - How to get the line of code that triggers a query? -


is there way (a gem, plugin or else) in rails 3.2 know line of code triggers database query? example in log have:

user load (0.4ms)  select `users`.* `users` `users`.`id` = 5 limit 1 

how can know line of code triggers query? thx...

i've found solution:

module querytrace   def self.enable!     ::activerecord::logsubscriber.send(:include, self)   end    def self.append_features(klass)     super     klass.class_eval       unless method_defined?(:log_info_without_trace)         alias_method :log_info_without_trace, :sql         alias_method :sql, :log_info_with_trace       end     end   end    def log_info_with_trace(event)     log_info_without_trace(event)     trace_log = rails.backtrace_cleaner.clean(caller).first     if trace_log && event.payload[:name] != 'schema'       logger.debug("   \\_ \e[33mcalled from:\e[0m " + trace_log)     end   end end 

in initializer add querytrace.enable!


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -