How to track and log slow queries in Ruby on Rails applications?

How to track and log slow queries in Ruby on Rails applications?

Slow SQL queries in Rails can be caught in development, before they become production fires. Using ActiveSupport::Notifications and the sql.active_record event, you build a small performance guard that logs every query above a time threshold straight to your console.