验证两列的唯一性 [英] Validate uniqueness of two columns

查看:142
本文介绍了验证两列的唯一性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证两个列在我的用户模型中是唯一的。我已经搜索Google,从我了解以下代码将无法正常工作:

I am trying to validate that two columns are unique in my User model. I've searched Google and from what I understand the following code will not work:

validates :login, :uniqueness=>true
validates :email, :uniqueness=>true

在我的情况下,验证电子邮件的唯一性,但不会验证登录的唯一性。我设置登录名在数据库上必须是唯一的。但是,如果已经存在,则会生成运行时错误。我不想在生产中出现错误,因为它会导致500内部服务器错误,用户不明白。

In my case doing that will validate the uniqueness of email, but will not validate the uniqueness of login. I set that login must be unique on the database. However, all that will do is generate a runtime error if it already exists. I don't want that error in production because it would result in a 500 internal server error, which the user would not understand.

有关如何验证

使用身份验证


  • CanCan

  • 设计

  • 设计邀请

更新:

从我可以解释的问题是由Devise引起的,特别是验证电子邮件和密码的可验证模块。

From what I can tell the problem is being caused by Devise, specifically the validatable module, which validate email and password.

更新2:

从Devise中删除可验证的模块将导致没有检查唯一性。

Removing the validatable module from Devise will result in neither checking for uniqueness.

更新3:

不会造成问题。相反,一个叫做Devise Invitable的宝石就是。删除此宝石会导致验证被触发。看来,这个gem试图强制验证电子邮件字段。由于这种宝石的性质,它是有意义的,但不应该被阻挡。

It appears that devise is not causing the problem. Rather, a gem called Devise Invitable is. Removing this gem results in the validation being triggered. It also appears that this gem tries to force validations of the email field. It makes sense due to the nature of this gem, but shouldn't be blocking.

推荐答案

问题在于devise_invitable宝石。在设计初始化器中,我必须设置

The problem was with the devise_invitable gem. In the devise initializer I had to set

  config.validate_on_invite = true

以确保验证已执行,默认值为false。我也不得不删除devise可验证模块。

to make sure the validations were executed, the default is false. I also had to remove the devise validatable module.

这篇关于验证两列的唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆