2

我有:

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)

我没有收到验证错误。相反,我有两条相同的记录。我究竟做错了什么?

4

1 回答 1

0

尝试

validates_uniqueness_of :day, scope: [:month,:year]
于 2016-07-17T23:04:04.900 回答