class ActiveRecord::Base before_validation :strip_whitespace_from_attributes!, :stringify_content_attributes! private # Will strip trailing and leading whitespace from all attributes and convert # empty strings to nil. def strip_whitespace_from_attributes! for col in self.class.content_columns.collect{|c| c.name.to_sym} if !self[col].nil? && self[col].respond_to?(:strip) s = self[col].strip.gsub("\r\n", "\n").gsub("\r", "\n").gsub("\r", "\n") s.size == 0 ? self[col] = nil : self[col] = s end end end # Will convert symbols to strings def stringify_content_attributes! for col in self.class.content_columns.collect{|c| c.name.to_sym} self[col] = self[col].to_s if self[col].is_a?(Symbol) end end end