dz6ttq
Last Updated: February 25, 2016
·
3.687K
· mchail
8381fa8ca53a0a8152df93af52b550f9

Log mongoid queries inline in rails console

Include the below text as an initializer. Name the file something like log_mongo_queries_in_rails_console.rb and place it in Rails.root/config/initializers.


# This initializer adds a method, show_mongo, when running a rails console. When active, all
# moped commands (moped is mongoid's mongodb driver) will be logged inline in the console output.
# If called again, logging will be restored to normal (written to log files, not shown inline).
# Usage:
#     > show_mongo

if defined?(Rails::Console)
    def show_mongo
        if Moped.logger == Rails.logger
            Moped.logger = Logger.new($stdout)
            true
        else
            Moped.logger = Rails.logger
            false
        end
    end
    alias :show_moped :show_mongo
end
Say Thanks
Respond

1 Response
Add your response

12653
7036c0acb78ea552f4084318a6f789de

Thank you for this tricks. It's pretty helpful for me :-)

over 1 year ago ·