如何验证范围的唯一性?

时间:2013-05-16 14:56:20

标签: mongodb mongoid

我有:

class Foo
  include Mongoid::Document

  field :year, :type => Integer, :default => Time.now.utc.year
  field :month, :type => Integer, :default => Time.now.utc.month
  field :day, :type => Integer, :default => Time.now.utc.day

  validates :day, :uniqueness => { :scope => [:month,:year] }
end

我做了以下两次:

Foo.create(:day => 24, :year => 2013, :month => 5)

我没有得到验证错误。相反,我有两个相同的记录。我做错了什么?

1 个答案:

答案 0 :(得分:0)

validates_uniqueness_of :day, scope: [:month,:year]