-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
None
When using accepts_nested_attributes_for in combination with a has_[one|many] relationship, any failed validations in the child specified are propagated upward to the parent, the document you're working with. This means the parent will never be valid if the child isn't.
This doesn't work when accepts_nested_attributes_for is used in combination with a belongs_to relation though:
class A include Mongoid::Document field :title, type: String validates_presence_of :title has_one :b, :inverse_of => :a end class B include Mongoid::Document belongs_to :a, :inverse_of => :b, :autosave => true accepts_nested_attributes_for :a end b = B.new #=> #<B _id: 4f33f9abe58d4960b1000001, _type: nil, a_id: nil> b.a_attributes = { :title => nil } b.a #=> #<A _id: 4f33fa70e58d4960b1000004, _type: nil, title: nil> b #=> #<B _id: 4f33fa48e58d4960b1000003, _type: nil, a_id: BSON::ObjectId('4f33fa70e58d4960b1000004')> b.a.valid? #=> false b.valid? #=> true b.save #=> true b.reload.a #=> nil