Jamie's Blog

Lessons from a life of startups, coding, countryside, and kids

PSA: Strip your user’s (email address)

Help preserve your sanity by strip'ing your user’s email address unless you enjoy being confused by things-that-should-work-but-don’t when confronted by a email address like ‘ jamie@ideasasylum.com’.

Rails makes this exceptionally easy by overriding the default setter:

class User < ActiveRecord::Base
  # ...
  def email=(email)
    write_attribute :email, email.strip
  end
end


describe User do
  it 'should strip the email' do
    user = User.new
    user.email = "   jamie@ideasasylum.com   "
    expect(user.email).to eq("jamie@ideasasylum.com")
  end
end