module to remove all whitespaces from several string columns

PHOTO EMBED

Thu Jun 10 2021 14:57:11 GMT+0000 (UTC)

Saved by @captm

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
content_copyCOPY