我有一个模型:
class Action
include Mongoid::Document
field :name, type: String
field :assignment_date, type: Date
...
index(
[
[ :name, Mongo::ASCENDING ],
[ :assignment_date, Mongo::ASCENDING ]
],
unique: true
)
validates_uniqueness_of [ :name, :assignment_date ]
但是,当我试图插入两个不同的文档,但名称相同、分配日期不同时,就会出现错误:
Mongoid::Errors::Validations - Validation failed - Name is already taken, Assignment date is already taken.:
我已经尝试了两个版本:
validates_uniqueness_of [ :name, :assignment_date ]
并且
validates_uniqueness_of :name, :assignment_date
如果我注释掉这行代码,一切都能正常工作。