module StripStringFieldsTrait as_trait do before_validation :strip_string_fields private def strip_string_fields string_columns = self.class.columns.select { |column| column.type == :string } string_columns.each do |string_column| attribute = string_column.name value = send(attribute) if value.present? send("#{attribute}=", value.strip) end end true end end end