email and new_email are two distinct columns. Every email should be unique, so if an email is added into either column it cannot already exist in either email or new_email columns.
Back story: I create the primary email for activated accounts and have a second new_email for when a user decides to change their email address but has not yet validated the new one via an email confirmation.
Most SO searches gave the scope solution:
I've tried validates :email, uniqueness: {scope: :new_email} and validates :new_email, uniqueness: {scope: :email} however I'm pretty sure this functionality acts to create a new key among the email1, email2 pair which is not the desired effect.
Currently I'm judging the validity of my code with the following two test cases (which are failing)
test "new_email should not match old email" do
@user.new_email = @user.email
assert_not @user.valid?
end
test "new_email addresses should be unique" do
duplicate_user = @user.dup
duplicate_user.new_email = @user.email.upcase
duplicate_user.email = @user.email + "z"
@user.save
assert_not duplicate_user.valid?
end