Mongodb 为什么不是';t使用“嵌入”进行验证?

Mongodb 为什么不是';t使用“嵌入”进行验证?,mongodb,mongoid,Mongodb,Mongoid,为什么验证不能与嵌入件一起工作 class Foo include Mongoid::Document embeds_one :bar, :cascade_callbacks => true end class Bar include Mongoid::Document embedded_in :foo field :test, :type => String field :year, :type => Integer, :default =&g

为什么验证不能与
嵌入件一起工作

class Foo
  include Mongoid::Document

  embeds_one :bar, :cascade_callbacks => true
end

class Bar
  include Mongoid::Document

  embedded_in :foo

  field :test, :type => String
  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 :year, :uniqueness => true, :presence => true, :scope => [:month, :day]
  # validates :day, :uniqueness => { :scope => [:month,:year] }
  validates_uniqueness_of :year, :scope => :day
end

Foo.create(:bar => { :test => 'asdf' }) # created document
Foo.create(:bar => { :test => 'asdf' }) # created document, no validation thrown!

为什么要多次创建Foo

关于验证
的唯一性,表示:

验证属性是否唯一。请注意,对于嵌入式文档,这将只检查字段在父文档上下文中是否唯一,而不是在整个数据库中

在本例中,在示例中创建了两个不同的文档。因此,Mongoid中的行为是正确的