# Invalid associated child object is silently not auto-saved and does not make saving of parent fail

**URL:** <https://discuss.rubyonrails.org/t/invalid-associated-child-object-is-silently-not-auto-saved-and-does-not-make-saving-of-parent-fail/62470>\
**Category:** rubyonrails-talk\
**Created:** [January 24, 2012, 10:47am UTC](https://discuss.rubyonrails.org/t/invalid-associated-child-object-is-silently-not-auto-saved-and-does-not-make-saving-of-parent-fail/62470 "2012-01-24T10:47:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Vandenabeele1](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@Peter\_Vandenabeele1](https://discuss.rubyonrails.org/u/Peter_Vandenabeele1)\
**Post date:** [January 24, 2012, 10:47am UTC](https://discuss.rubyonrails.org/t/invalid-associated-child-object-is-silently-not-auto-saved-and-does-not-make-saving-of-parent-fail/62470/1 "2012-01-24T10:47:17Z")

</div>

TL;DR: child.valid? == false parent.save #=\> true child.new\_record? #=\> true child is not saved, but parent _is_ saved I had expected this to block saving of parent

Hi,

I am confused by this behavior (ruby 1.9.3 Rails 3.2.0):

class Parent \< ActiveRecord::Base

has\_one :child

end

class Child \< ActiveRecord::Base validates :name, :presence =\> true end

$ rails c Loading development environment (Rails 3.2.0) 1.9.3-p0 :001 \> p = Parent.new(:name =\> “dad”)

=\> #\<Parent id: nil, name: “dad”, created\_at: nil, updated\_at: nil\>

1.9.3-p0 :002 \> p.child = Child.new(:name =\> “Sarah”) (0.2ms) BEGIN (0.2ms) COMMIT =\> #\<Child id: nil, name: “Sarah”, parent\_id: nil, created\_at: nil, updated\_at: nil\>

1.9.3-p0 :003 \> p.save! (0.2ms) BEGIN SQL (4.8ms) INSERT INTO “parents” (“created\_at”, “name”, “updated\_at”) VALUES ($1, $2, $3) RETURNING “id” [[“created\_at”, Tue, 24 Jan 2012 10:06:59 UTC +00:00], [“name”, “dad”], [“updated\_at”, Tue, 24 Jan 2012 10:06:59 UTC +00:00]]

SQL (0.8ms) INSERT INTO “children” (“created\_at”, “name”, “parent\_id”, “updated\_at”) VALUES ($1, $2, $3, $4) RETURNING “id” [[“created\_at”, Tue, 24 Jan 2012 10:06:59 UTC +00:00], [“name”, “Sarah”], [“parent\_id”, 2], [“updated\_at”, Tue, 24 Jan 2012 10:06:59 UTC +00:00]]

(9.8ms) COMMIT =\> true

# both are saved as expected (with child “auto-saved”)

1.9.3-p0 :004 \> m = Parent.new(:name =\> “mom”) =\> #\<Parent id: nil, name: “mom”, created\_at: nil, updated\_at: nil\>

1.9.3-p0 :005 \> m.child = Child.new(:name =\> nil) # EMPTY NAME (0.2ms) BEGIN (0.2ms) COMMIT =\> #\<Child id: nil, name: nil, parent\_id: nil, created\_at: nil, updated\_at: nil\> 1.9.3-p0 :006 \> m.valid?

=\> true 1.9.3-p0 :007 \> m.child.valid? =\> false

# child is not valid (name is not present)

1.9.3-p0 :008 \> m.save! (0.2ms) BEGIN SQL (0.5ms) INSERT INTO “parents” (“created\_at”, “name”, “updated\_at”) VALUES ($1, $2, $3) RETURNING “id” [[“created\_at”, Tue, 24 Jan 2012 10:07:42 UTC +00:00], [“name”, “mom”], [“updated\_at”, Tue, 24 Jan 2012 10:07:42 UTC +00:00]]

(12.3ms) COMMIT =\> true

# the save of the parent happily continues and the child is silently not auto-saved.

I had expected that the entire save! would have failed in a transaction, so that either ALL or NOTHING are saved.

When I add to the model e.g. the `:autosave => true` option, I get the expected behavior:

class Parent \< ActiveRecord::Base

has\_one :child, :autosave =\> true

end

class Child \< ActiveRecord::Base validates :name, :presence =\> true end

$ rails c Loading development environment (Rails 3.2.0) 1.9.3-p0 :001 \> # with :autosave =\> true on the `has_one :child` association

1.9.3-p0 :002 \> m = Parent.new(:name =\> “mom”) =\> #\<Parent id: nil, name: “mom”, created\_at: nil, updated\_at: nil\> 1.9.3-p0 :003 \> m.valid? =\> true 1.9.3-p0 :004 \> m.child = Child.new(:name =\> nil)

(0.1ms) BEGIN (0.1ms) COMMIT =\> #\<Child id: nil, name: nil, parent\_id: nil, created\_at: nil, updated\_at: nil\> 1.9.3-p0 :005 \> m.valid? =\> false

# it seems `:autosave => true` also implies `validates_associated` on the association ?

1.9.3-p0 :006 \> m.child.valid? =\> false 1.9.3-p0 :007 \> m.save! (0.2ms) BEGIN (0.2ms) ROLLBACK ActiveRecord::RecordInvalid: Validation failed: Child name can’t be blank …

Next to `:autosave => true`, also using `validates_associated :child` or `accepts_nested_attributes_for` all result in the behavior I had expected (save does “all or nothing”).

But, I would expect the standard functionality (without :autosave =\> true or

validates\_associated) to not save anything (neither parent or children) in the transaction when one of the objects for saving is invalid.

I feel the current behavior allows a “silent” failure where only half of the expected

objects is saved while the save(!) returns success.

I am not pleading to make `:autosave => true` or `validates_associated` the default on all associations.

I am pleading for the “ad-hoc” measure that

- if ActiveRecord decides to auto-save associated objects together with the main object
- and one of thos auot-saves fails on any of those associated objects
- then the entire transaction is rolled back and a non-success result is returned

If there is interest in this, I may look in the code and try to find the place to fix it, but maybe there are fundamental reasons for the way it works today.

Thanks for your time,

Peter

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 25, 2012, 10:31am UTC](https://discuss.rubyonrails.org/t/invalid-associated-child-object-is-silently-not-auto-saved-and-does-not-make-saving-of-parent-fail/62470/2 "2012-01-25T10:31:42Z")

</div>

Hai!

One-to-one

use has\_one in the base, and belongs\_to in the associated model.

class Parent \< ActiveRecord::Base &nbsp;&nbsp;has\_one :child end

class Child \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :parent # foreign key - parent\_id end

Try this way:

Bye:) Bdeveloper01

---

<div class="post-metadata">

**Author:** ![Peter\_Vandenabeele1](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@Peter\_Vandenabeele1](https://discuss.rubyonrails.org/u/Peter_Vandenabeele1)\
**Post date:** [January 25, 2012, 12:15pm UTC](https://discuss.rubyonrails.org/t/invalid-associated-child-object-is-silently-not-auto-saved-and-does-not-make-saving-of-parent-fail/62470/3 "2012-01-25T12:15:43Z")

</div>

Thank you for the feedback. I tried and this is the result:

class Child \< ActiveRecord::Base has\_one :parent validates :name, :presence =\> true end

class Parent \< ActiveRecord::Base

has\_one :child end

$ rails c Loading development environment (Rails 3.2.0) 1.9.3-p0 :001 \> p = Parent.new =\> #\<Parent id: nil, name: nil, created\_at: nil, updated\_at: nil\> 1.9.3-p0 :002 \> c1 = p.build\_child

(0.2ms) BEGIN (0.2ms) COMMIT =\> #\<Child id: nil, name: nil, parent\_id: nil, created\_at: nil, updated\_at: nil\> 1.9.3-p0 :003 \> p.valid? =\> true 1.9.3-p0 :004 \> c1.valid? =\> false

1.9.3-p0 :005 \> p.save (0.2ms) BEGIN SQL (5.8ms) INSERT INTO “parents” (“created\_at”, “name”, “updated\_at”) VALUES ($1, $2, $3) RETURNING “id” [[“created\_at”, Wed, 25 Jan 2012 11:58:00 UTC +00:00], [“name”, nil], [“updated\_at”, Wed, 25 Jan 2012 11:58:00 UTC +00:00]]

(24.2ms) COMMIT =\> true

The line above reports “success” (true), while the child is not auto-saved

Now trying to implicitly save a non-valid child with a straight assignment.

1.9.3-p0 :022 \> p.child = Child.new (0.2ms) BEGIN (0.4ms) UPDATE “children” SET “parent\_id” = NULL, “updated\_at” = ‘2012-01-25 12:08:36.168342’ WHERE “children”.“id” = 4

(0.2ms) ROLLBACK ActiveRecord::RecordNotSaved: Failed to save the new associated child. from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated\_validations/gems/activerecord-3.2.0/lib/active\_record/associations/has\_one\_association.rb:23:in `block in replace’

```
from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:190:in `transaction'
from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/transactions.rb:208:in `transaction'

from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/associations/has_one_association.rb:11:in `replace'
from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/associations/singular_association.rb:17:in `writer'

from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/associations/builder/association.rb:51:in `block in define_writers'

```

I would have expected this behavior (an exception for p.save!) that is

happening for an explicit save of an associated child, to also occur on an “auto-save” of an associated child.

Thanks,

Peter

---

<div class="post-metadata">

**Author:** ![Peter\_Vandenabeele1](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@Peter\_Vandenabeele1](https://discuss.rubyonrails.org/u/Peter_Vandenabeele1)\
**Post date:** [January 25, 2012, 12:23pm UTC](https://discuss.rubyonrails.org/t/invalid-associated-child-object-is-silently-not-auto-saved-and-does-not-make-saving-of-parent-fail/62470/4 "2012-01-25T12:23:49Z")

</div>

Sorry, that should have been Child belongs\_to :parent …

But, the results are the same. Trying again:

…/app/models$ cat \* class Child \< ActiveRecord::Base

belongs\_to :parent validates :name, :presence =\> true end class Parent \< ActiveRecord::Base has\_one :child end

$ rails c Loading development environment (Rails 3.2.0) 1.9.3-p0 :001 \> p = Parent.new

=\> #\<Parent id: nil, name: nil, created\_at: nil, updated\_at: nil\> 1.9.3-p0 :002 \> c1 = p.build\_child (0.2ms) BEGIN (0.2ms) COMMIT =\> #\<Child id: nil, name: nil, parent\_id: nil, created\_at: nil, updated\_at: nil\>

1.9.3-p0 :003 \> p.valid? =\> true 1.9.3-p0 :004 \> c1.valid? =\> false 1.9.3-p0 :005 \> p.save (0.2ms) BEGIN SQL (5.7ms) INSERT INTO “parents” (“created\_at”, “name”, “updated\_at”) VALUES ($1, $2, $3) RETURNING “id” [[“created\_at”, Wed, 25 Jan 2012 12:18:32 UTC +00:00], [“name”, nil], [“updated\_at”, Wed, 25 Jan 2012 12:18:32 UTC +00:00]]

(21.9ms) COMMIT =\> true 1.9.3-p0 :006 \> p.child = Child.new (0.2ms) BEGIN (0.2ms) ROLLBACK ActiveRecord::RecordNotSaved: Failed to save the new associated child. from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated\_validations/gems/activerecord-3.2.0/lib/active\_record/associations/has\_one\_association.rb:23:in `block in replace’

```
from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:190:in `transaction'
from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/transactions.rb:208:in `transaction'

from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/associations/has_one_association.rb:11:in `replace'
from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/associations/singular_association.rb:17:in `writer'

from /home/peterv/.rvm/gems/ruby-1.9.3-p0@associated_validations/gems/activerecord-3.2.0/lib/active_record/associations/builder/association.rb:51:in `block in define_writers'

```

Thanks,

Peter
