【问题标题】:Rails Validates_uniqueness of scope not workingRails Validates_uniqueness of scope 不起作用
【发布时间】:2012-07-10 07:46:12
【问题描述】:

我的验证在 create 方法上引发了“已发送电子邮件”错误。

我的客户模型中有这个验证:

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

这就是我在日志中看到的:

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

感谢您的帮助。

【问题讨论】:

  • 看起来您尝试使用相同的 account_id = 2,不是吗?

标签: ruby-on-rails validation scope validates-uniqueness-of


【解决方案1】:

您的范围正在将电子邮件搜索范围缩小到给定的 account_id (= 2)。不确定这是否是您的预期行为,但如果您希望它也搜索其他帐户 ID,则应删除范围。

验证失败,因为您输入的电子邮件已被占用。因此,他们正在做你告诉他们做的事情......

【讨论】:

    猜你喜欢
    • 2021-07-30
    • 2012-09-10
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2014-05-14
    • 1970-01-01
    相关资源
    最近更新 更多