User.email has uniqueness incompatible with soft_deletion
Soft deleting a user and then trying to create a new user with the same email address, will raise a ActiveRecord::RecordNotUnique
exeption.
ActiveRecord::RecordNotUnique at /users
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_email"
DETAIL: Key (email)=(social_worker@example.com) already exists.
: INSERT INTO "users" ("email", "encrypted_password", "role", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"
Solution:
Create a partial index on the email field in the users table like this:
add_column :users, :active, :boolean
add_index :users, :email, unique: true, where: 'active'