Log every SQL query to database in Rails 3 -
this question follow question, should place code?
connection = activerecord::base.connection class << connection alias :original_exec :execute def execute(sql, *name) # try log sql command ignore errors occur in block # log before executing, in case execution raises error begin file = file.open(rails_root + "/log/sql.txt",'a'){|f| f.puts time.now.to_s+": "+sql} rescue exception => e ; end # execute original statement original_exec(sql, *name) end end
i have tried placing inside of model, happens when execute sql query more once returns "stack level deep" error.
put in config/initializers. it's because of reloading classes each time in dev env. code need executed once though.
Comments
Post a Comment