class Member < ApplicationRecord
before_validation :normalize_email
validates :email, presence: true, uniqueness: { case_sensitive: false }
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
private
def normalize_email
self.email = email.to_s.strip.downcase
end
end
Normalize before validation to avoid “same email, different casing/whitespace” bugs. Keep normalization deterministic and small; put it in the model so imports, consoles, and controllers all behave the same.