How to Turn Deprecation Warnings Off in Rails

Posted by ryan
at 4:19 PM on Monday, December 04, 2006

I was talking with a friend of mine last week about the new Rails deprecation warnings that you’ll see when you do naughty things like access @cookies from your controller or use deprecated methods like ActiveRecord’s find_all. Sometimes it’s useful to turn off those warnings – especially when they’re coming from a part of your application you don’t control (i.e. a third-party library).

To turn off all deprecation warnings, just do the following:

ActiveSupport::Deprecation.silenced = true

Or, if you want to perform something other than spit out deprecation warnings you can re-route them however you want within a block:

ActiveSupport::Deprecation.behavior = Proc.new { |msg, stack| MyLogger.warn(msg) }
Or, if you want to be more granular, you can silence specific parts of your code that you know reference deprecated code:

def bad_action
  ActiveSupport::Deprecation.silence { p "Referencing #{@cookies} is bad" }
end

Hopefully this will get you past any annoying deprecations you can’t do much about (without giving you an easy out for those that you can do something about).

Comments

Leave a response

  1. Rob SanheimDecember 05, 2006 @ 11:18 PM
    I recently updated my company's app to 1.2, and the deprecations were a bit of a pain in spots. THere is also the annoying bug where if you hit certain errors in a functional test (I think method_missing is one), you'll get about two pages of complaints about @request and @response, even if your code is clean. Must be some place where the test process doesn't silence deprecations when it should. Anyways here are my experiences on the 1.2 upgrade, might be helpful for others: http://www.robsanheim.com/2006/11/30/updating-a-rails-app-for-12-notes-and-tips/