Created
January 5, 2011 13:54
-
-
Save tylerhunt/766340 to your computer and use it in GitHub Desktop.
A Rails plugin for stripping strings on models and replacing empty strings with nil values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CoerceBlanks | |
def write_attribute(attr_name, value) | |
if value.is_a?(String) | |
value.strip! | |
value = nil if value.empty? | |
end | |
super | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveRecord::Base.class_eval do | |
include CoerceBlanks | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment