Wednesday, April 30, 2008

Turn Deprecation Warnings Off in Rails

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

1 comment:

Balaji Ananthachari said...

Nice post ...

Would like to see more from you.