1

I am seeing the following SQL being used to check that an email address (e.g. joebloggs@joebloggs.com) 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: joebloggs@joebloggs.com\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 'joebloggs@joebloggs.com') LIMIT 1

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

validates_uniqueness_of :email

Thanks in advance for your help.

Neil
  • 21
  • 3
  • Is there any reason you are not letting devise validate your model? – Justin Wood Jan 02 '14 at 22:34
  • What kind DBMS do you have? – Zane Jan 02 '14 at 22:41
  • @Zane: The backticks in the SQL indicate that it is MySQL, so does the "On the attempted record creation in MySQL" in the question body. – mu is too short Jan 02 '14 at 23:19
  • 1
    `'--- !ruby/object:Mail::Multibyte::Chars ...` is a YAMLized `Mail::Multibyte::Chars` object, not the String that your model is probably expecting it to be. Where does the email address come from? Have you looked at http://stackoverflow.com/q/14824179/479863 ? – mu is too short Jan 02 '14 at 23:24

1 Answers1

-1

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
Htoo Myat Aung
  • 666
  • 1
  • 7
  • 16