Here’s something I just whipped up to quiet ActiveRecord’s logging in development mode. Normally, ActiveRecord’s AbstractAdapter logs every SQL query as it is made. Sometimes, it’s nice to quiet this down without changing your log level while tracking down a problem.
class ActiveRecord::ConnectionAdapters::AbstractAdapter def log_info_with_silencing(sql, name, runtime) unless ActiveRecord::Base.silence_sql_logging log_info_without_silencing(sql, name, runtime) end end alias_method_chain :log_info, :silencing end module SqlSilencer def silence_sql logging = ActiveRecord::Base.silence_sql_logging begin ActiveRecord::Base.silence_sql_logging = true yield ensure ActiveRecord::Base.silence_sql_logging = logging end end end class ActiveRecord::Base @@silence_sql_logging = false cattr_accessor :silence_sql_logging include SqlSilencer end class ActionController::Base include SqlSilencer end
Drop this into a file in config/initializers if you’re on Rails 2.0 or require it from within environment.rb for pre-2.0.
To use inside a controller action or model method, simply wrap the code you want to silence inside a block:
def index silence_sql do # lots of work here... end
One interesting side effect of this is that you can easily see how much database work is done as a result of what’s in your view. Wrap the entire controller action inside a silence_sql block, then look at your log. Any queries you see are a result of things you didn’t load in the controller.