class ActiveRecord::Base before_validation :strip_text_attributes private def strip_text_attributes each_text_attribute do |attribute, value| self[attribute] = value.strip end end def each_text_attribute self.class.columns.select(&:text?).map(&:name).each do |attribute| value = self[attribute] yield attribute, value if value.is_a?(String) end end end