扶手:验证多列的唯一性扶手、唯一性

2023-09-08 15:44:37 作者:迷人又混蛋

有没有轨道路的方式来验证实际的记录是唯一的,不只是一个列?例如,友谊型号/表不应该能够有多个相同的记载,如:

  USER_ID:10 | friend_id:20
USER_ID:10 | friend_id:20
 

解决方案

您可以范围 validates_uniqueness_of 呼叫如下。

validates_uniqueness_of:USER_ID,:范围=> :friend_id 楼梯扶手用不锈钢还是木头好 听老司机告诉你,别被坑了都不知道

Is there a rails-way way to validate that an actual record is unique and not just a column? For example, a friendship model / table should not be able to have multiple identical records like:

user_id: 10 | friend_id: 20
user_id: 10 | friend_id: 20

解决方案

You can scope a validates_uniqueness_of call as follows.

validates_uniqueness_of :user_id, :scope => :friend_id