0

这是我的模型文件。使用范围的日期时间字段的唯一性验证

class Attendance < ActiveRecord::Base 
belongs_to :staff, class_name: 'User', foreign_key: 'staff_id', inverse_of: :attendances 
belongs_to :child, inverse_of: :attendances 

validates :attendance_date, uniqueness: {scope: :child} 

end 

我的主要目标是不允许在同一日期和child_id被保存在database.The验证我写的不是在这种情况下工作。

attendance_date是一个日期时间字段。

请帮我解决这个问题!

+0

尝试'{范围:child_id}' –

回答

1

使用范围验证唯一性是为唯一性添加多个列约束。您可以添加列的你想使用的应用的独特性:

试试这个:

validates :attendance_date, uniqueness: {scope: :child_id}