2

My validations raise an "Email already taken" error on the create method.

I have this validation in my Customer model:

validates :email, :uniqueness => {:scope => :account_id, :case_sensitive => false} 

and this is what I'm seeing in my logs:

Customer Load (0.4ms)  SELECT `customers`.* FROM `customers` WHERE `customers`.`account_id` = 2 AND `customers`.`email` = 'xxxxx@gmail.com' LIMIT 1
(0.1ms)  BEGIN
Customer Exists (0.8ms)  SELECT 1 AS one FROM `customers` WHERE `customers`.`email` = BINARY 'xxxxx@gmail.com' LIMIT 1
Customer Exists (0.3ms)  SELECT 1 AS one FROM `customers` WHERE (`customers`.`email` = 'xxxxx@gmail.com' AND `customers`.`account_id` = 2) LIMIT 1
(0.2ms)  ROLLBACK

Thanks for the help.

tbrooks
  • 85
  • 1
  • 9

1 Answers1

0

Your scope is narrowing the email search to a given account_id (= 2). Not sure if this is your intended behavior, but you should remove the scope if you want it to search other account ids as well.

The validations are failing because the email you've entered is already taken. Thus, they are doing exactly what you've told them to do...

Tyler
  • 11,272
  • 9
  • 65
  • 105