0

で検証が機能しないのはなぜembeds_oneですか?

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 が複数回作成されるのはなぜですか?

4

1 に答える 1

1

に関してvalidates_uniqueness_ofMongoid の文書には次のように書かれています。

属性が一意であることを検証します。埋め込みドキュメントの場合、これはフィールドがデータベース全体ではなく、親ドキュメントのコンテキスト内で一意であることのみを確認することに注意してください。

あなたの場合、この例では 2 つの異なるドキュメントが作成されます。したがって、動作は Mongoid で正しいです。

于 2013-05-17T22:02:05.143 に答える