Hi All,
I'm developing an application using Ruby on Rails.
I would like to erase old queries in the ActiveRecord::Base.logger object every time when i call a new action, essentially where ENV = production.
the goal is not to erase all queries like using config.log_level :info. i need only last queries to build a file whith those quiries. Here some code:
in the lib:
module SqlHunter
class ActiveRecord::ConnectionAdapters::AbstractAdapter
@@queries = [] # without this line it work perfectly
@@logging = false
cattr_accessor :queries, :logging
def log_info_with_trace(sql, name, runtime)
@@queries << sql if @@logging
end
alias_method_chain :log_info, :trace
end
end
in the controller (report action)
ActiveRecord::ConnectionAdapters::AbstractAdapter::logging = true
.....
sqlfile = File.open("public/advancedStats/#{@dir_name}/advancedStatQuery.sql", 'w')
@queries = ActiveRecord::ConnectionAdapters::AbstractAdapter::queries
for query in @queries do
sqlfile.write("#{query} \n")
end
sqlfile.close
i asked an old related question here
Thanks to Tamás Mezei and Damien MATHIEU for their last answer
Mondher