提问人:Milind 提问时间:9/13/2023 更新时间:9/13/2023 访问量:28
Rails 5 - 根据两个动态属性的范围验证设计用户电子邮件
Rails 5 - Validate devise users email based on scope of two dynamic attributes
问:
如何根据某些条件验证用户电子邮件,从而覆盖设计默认的 VALIDATABLE 功能。
用户表具有company_id(具有一个作为所有者的角色)和institution_id(具有两个角色作为导师和学生),belongs_to:role model
那么我怎样才能实现以下场景——
- 如果电子邮件为 [email protected] 的用户是公司名称 Apple 的所有者(角色),则必须允许他注册。
- 电子邮件为 [email protected] 的用户如果是属于 Apple 公司的名为 Apple-1 的机构的导师,也必须被允许注册。
- 电子邮件 [email protected] 的用户如果是属于 Apple 公司的名为 Apple-1 的机构的学生,也必须被允许注册。
如何检查电子邮件的唯一性并根据上述条件确定其范围。 我不想搞砸 devise 的默认可验证模块,也不想更新我在 devise.rb 中的身份验证密钥,除了电子邮件和用户名。
我可以有条件地实现这一点,因为每次我看到电子邮件都会检查唯一性,而不管我添加了什么代码,所以每次电子邮件注册都会失败,因为它总是在没有添加任何范围的情况下被发现。user.rb
in user.rb
##it runs but of no use as default email check is also ran by default.
validates :institution_id, uniqueness: {
scope: [ :company_id, :email ],
message: "Email exists for the institution"
}
答: 暂无答案
评论