Created
January 24, 2014 22:17
-
-
Save alkalinecoffee/8607900 to your computer and use it in GitHub Desktop.
Rails: Clean (strip) all String attributes before saving
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
# lib/core_ext/active_record_extensions.rb | |
module ActiveRecordExtensions | |
extend ActiveSupport::Concern | |
def clean_strings! | |
self.attributes.select{|key, val| val.class == String}.each {|key, val| val.strip!} | |
end | |
included do | |
before_validation :clean_strings! | |
end | |
end | |
ActiveRecord::Base.send(:include, ActiveRecordExtensions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment