小编ker*_*loz的帖子

如何使用 Rails 验证 MongoDB 中模型的唯一性?

在我的 rails 应用程序中,我使用 MongoDB。我有 4 个模型,包括管理员。所有实体都有一个电子邮件地址。我需要进行验证以确保电子邮件对于所有模型都是唯一的。

除了在每个模型中搜索电子邮件之外,还有更简单/更好的方法吗?

喜欢


 def email_uniquness_across_models(email)
    User.where(email: email).exists? ||
      Admin.where(email: email).exists? ||
      Transporter.where(email: email).exists? ||
      Company.where(email: email).exists?
  end

Run Code Online (Sandbox Code Playgroud)

ruby-on-rails mongodb mongoid

5
推荐指数
1
解决办法
118
查看次数

标签 统计

mongodb ×1

mongoid ×1

ruby-on-rails ×1