我有模型:
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 ]
但是,当我尝试插入具有相同名称但不同assigned_date 的2个不同文档时,我收到错误消息:
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
如果我评论这条线一切正常。