mongoid/mongoid — Issue #1697: accepts_nested_attributes_for child validation not respected when specified relation is belongs_to
Author: DouweM
Created: 2012-02-09 17:03:19+00:00
State: open

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:

```ruby
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.a_attributes = { :title => nil }
b.a #=> #
b #=> #

b.a.valid? #=> false
b.valid? #=> true

b.save #=> true
b.reload.a #=> nil
```