Ruby validates_uniqueness_of email leading to wrong SQL uniqueness check

253 views Asked by At

I am seeing the following SQL being used to check that an email address (e.g. [email protected]) is unique when a user signs up on my Ruby website:

SELECT `users`.id FROM `users` WHERE (`users`.`email` = BINARY '--- !ruby/object:Mail::Multibyte::Chars \nwrapped_string: [email protected]\n') LIMIT 1

This is always resulting in zero rows being returned, so Ruby attempts to create the user in the database. On the attempted record creation in MySQL, it fails because users.email has a unique index on it.

Can anyone tell me why Ruby is generating the SQL statement above? It does this on my live site running in production or development mode. On my development site (running in production or development mode), the following (correct) SQL is generated:

SELECT `users`.id FROM `users` WHERE (`users`.`email` = BINARY '[email protected]') LIMIT 1

For user management, I am using devise with the following setting:

validates_uniqueness_of :email

Thanks in advance for your help.

1

There are 1 answers

0
Htoo Myat Aung On

In your user model you can add email validation to uniqueness as false.

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable

  devise :database_authenticatable,:registerable,
         :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:email]

  validate :email, presence: true, uniqueness: false

end