# Two unsaved related objects

**URL:** <https://www.ruby-forum.com/t/two-unsaved-related-objects/81939>\
**Category:** Rails\
**Created:** [March 14, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/two-unsaved-related-objects/81939 "2006-03-14T23:17:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 14, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/two-unsaved-related-objects/81939/1 "2006-03-14T23:17:53Z")

</div>

Given the two tables Parent, Child where Parent has\_many children:  
If I don’t save parent first then it seems that child.parent will not  
be assigned automatically if I call parent.children \<\< child . Must  
the parent record be saved before I call parent.children \<\< child ?

If I must save Parent first then can someone explain to me how I can  
call parent.valid? before it is saved and have the children validated  
too?

Any workaround I see requires a lot of coercing of these  
relationships together over and over again.

-John

–  
John S.  
Computing Staff - Webmaster  
Kavli Institute for Theoretical Physics  
University of California, Santa Barbara  
removed\_email\_address@domain.invalid  
(805) 893-6307

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 15, 2006, 10:46am UTC](https://www.ruby-forum.com/t/two-unsaved-related-objects/81939/2 "2006-03-15T10:46:32Z")

</div>

Am Dienstag, den 14.03.2006, 14:16 -0800 schrieb John S.:

> Given the two tables Parent, Child where Parent has\_many children:  
> If I don’t save parent first then it seems that child.parent will not  
> be assigned automatically if I call parent.children \<\< child . Must  
> the parent record be saved before I call parent.children \<\< child ?

As Child belongs\_to Parent, the foreign key is stored in the children  
table. If Parent is a new record and has no id, the children can’t save  
it as foreign key.

> If I must save Parent first then can someone explain to me how I can  
> call parent.valid? before it is saved and have the children validated  
> too?

if !parent.valid? || children.all? { |c| c.valid? }

# some records are not valid

else  
parent.save  
parent.children \<\< \*children  
end

So you never get a saved parent unless all children are valid, too.

–  
Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 15, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/two-unsaved-related-objects/81939/3 "2006-03-15T17:44:58Z")

</div>

Using that form of validation means I cannot validate whether a child  
has a parent or not. Isn’t that a problem?

-John

–  
John S.  
Computing Staff - Webmaster  
Kavli Institute for Theoretical Physics  
University of California, Santa Barbara  
removed\_email\_address@domain.invalid  
(805) 893-6307
