您如何使用 Rails 3 验证根据两个值确定唯一性? [英] How do you scope uniquenuess based on two values with Rails 3 validations?

查看:35
本文介绍了您如何使用 Rails 3 验证根据两个值确定唯一性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Replay 的模型,它有 3 个属性:match_id、game_number 和 uploader_id.本质上,我想要一个验证来强制上传者不会上传与他们已经为该比赛上传的重播相同游戏编号的比赛重播.但是,我希望其他用户能够上传该比赛和比赛编号的重播.

I have an model, called Replay, that has 3 attributes, match_id, game_number, and uploader_id. Essentially, I would like a validation to enforce that an uploader does not upload a replay for a match with the same game number as a replay they have already uploaded for that match. I would like, however, for another user to be able to upload a replay for that match and game number.

我现在使用的验证是:

validates :game_number, presence: true, uniqueness: { scope: :match_id }

但这并没有考虑到uploader_id.任何帮助,将不胜感激.

But this does not take into account the uploader_id. Any help would be appreciated.

推荐答案

试试这个:

validates :game_number, presence: true, uniqueness: { scope: [:match_id, :uploader_id] }

这篇关于您如何使用 Rails 3 验证根据两个值确定唯一性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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