class Order
include Mongoid::Document
include Mongoid::Timestamps
#relationships
embeds_one :user_detail
#fields
field :description
#validations
validates :user_detail, presence: true
end
これは順番に埋め込まれたオブジェクトです:
class UserDetail
include Mongoid::Document
include Mongoid::Timestamps
#fields
field :name, :type => String
field :zip_code, :type => String
field :email, :type => String
# Relationships
embedded_in :order
#validations
validates_presence_of :name, :zip_code, :email
end
オブジェクトオブジェクトを使用してmongodborder
オブジェクトを保存/永続化したい。user_detail
embedded_in
order
私は試してみました:
order = Order.new(description: "checking description")
order.user_detail = Order.new(:name => "John", :zip_code => "26545", :email => "john@john.com")
order.save!
しかし、検証に失敗します:
o.save!
Mongoid::Errors::Validations:
Problem:
Validation of Order failed.
Summary:
The following errors were found: User detail is invalid
Resolution:
Try persisting the document with valid data or remove the validations....
この問題を解決するにはどうすればよいですか? 私はモンゴイド3.xを使用しています