How to debug why database queries are occuring in mongoid

235 views
Skip to first unread message

declan

unread,
Mar 21, 2011, 6:19:11 PM3/21/11
to Mongoid
Hi folks,

I've just been tracking down the cause of queries occurring when I
actually expected no query to occur. I was wondering is there an easy
way to 'tail' the log of queries happening in real time as I step
through the code of a controller or model with the debugger - I'd like
something like the MySQL query log.

Is there an easier way than why I've posted below (which basically
intercepts the debug messages)...

Kind regards,
Declan

* Open path/to/gems/ruby-1.9.2-head/gems/mongoid-2.0.0.beta.20/lib/
mongoid/logger.rb in a text editor
* Remove :debug and :info from the list of delegates
* Add them as methods to the class
...
def debug(message)
debugger
logger.debug(message) if logger && logger.respond_to?(:debug)
end

def info(message)
debugger
logger.info(message) if logger && logger.respond_to?(:info)
end
...
* Start the Rails server in debug mode "rails s --debug"
* Whenever a mongoid query is run you will end up in the debug()
method
* You can print out 'message' to see what query is being called ("puts
message")
* If you type "backtrace" you will see the call stack
* Generally if you scroll up to about line #26 in the stack trace it
should give you an idea of what is triggering the database call
* Type "c" when you want to run onto the next call
Reply all
Reply to author
Forward
0 new messages