Monkeypatching ActiveRecord so 'logger' Works Seamlessly

Posted on February 18, 2008

Ever tried calling “logger.info” from an ActiveRecord object, only to have rails lay the smack-down on ya?

Use ‘logger’ seamlessly throughout your app with this monkey patch:

module ActiveRecordExtensions

  def logger
    RAILS_DEFAULT_LOGGER
  end

end

class ActiveRecord::Base
  include ActiveRecordExtensions
end

Throw that in environment.rb or wherever you keep your monkey patches, and let the good times roll.

Rails ActiveRecord save vs. save!

Posted on October 09, 2007

The subtle difference between these two eluded me for a while, so for those who are just getting started in Rails this tip may be helpful.

If you want to ensure that no ActiveRecord validation errors, etc. were generated when saving a model, do a:
foo.save!
If you do the following instead:
foo.save

The method will return false but an exception will not be generated. I’ve debugged several hair-pulling issues that could’ve been avoided by calling “save!” .