Skip to content

Instantly share code, notes, and snippets.

@alkalinecoffee
Created January 24, 2014 22:17
Show Gist options
  • Save alkalinecoffee/8607900 to your computer and use it in GitHub Desktop.
Save alkalinecoffee/8607900 to your computer and use it in GitHub Desktop.
Rails: Clean (strip) all String attributes before saving
# 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