# Validates\_presence\_of in parent ok. but how in children?

**URL:** <https://www.ruby-forum.com/t/validates-presence-of-in-parent-ok-but-how-in-children/83162>\
**Category:** Rails\
**Created:** [January 23, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/validates-presence-of-in-parent-ok-but-how-in-children/83162 "2007-01-23T23:33:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dweinand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f17d59/32.png) [@dweinand](https://www.ruby-forum.com/u/dweinand)\
**Post date:** [January 23, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/validates-presence-of-in-parent-ok-but-how-in-children/83162/1 "2007-01-23T23:33:43Z")

</div>

i have the problem that my child class fields aren’t validated on saving  
the  
parent. working with parent and child is fine.  
if all fields are filled correctly everything is saved as expected.  
but missing fields in the child arent added to the errors.  
following short sample shows what i mean:

## Model

class Parent \< ActiveRecord::Base  
has\_one :children  
validates\_presence\_of :parent\_name

def init!  
self.build\_children  
end  
end

class Children \< ActiveRecord::Base  
belongs\_to :parent  
validates\_presence\_of children\_name  
end

## controller

@parent = session[:parent] ||= Parent.new  
@parent.init!  
@parent.save

=\>  
the absence of parent\_name will be correctly shown @parent.errors  
but i cant find the absence of children\_name. isnt it validated?

if parent\_name is set and children name is nullable in DB  
everything will be saved without validating the children.

i think im missing something important at this pont.

any help?

---

<div class="post-metadata">

**Author:** ![dweinand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f17d59/32.png) [@dweinand](https://www.ruby-forum.com/u/dweinand)\
**Post date:** [January 24, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/validates-presence-of-in-parent-ok-but-how-in-children/83162/2 "2007-01-24T22:25:48Z")

</div>

dweinand wrote:

> has\_one :children  
> end
> 
> if parent\_name is set and children name is nullable in DB  
> everything will be saved without validating the children.

You need to add

validates\_associated :children

to your Parent class.

(You’d get this automatically if it was a has\_many rather than a  
has\_one.)

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![dweinand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f17d59/32.png) [@dweinand](https://www.ruby-forum.com/u/dweinand)\
**Post date:** [January 25, 2007, 12:06am UTC](https://www.ruby-forum.com/t/validates-presence-of-in-parent-ok-but-how-in-children/83162/3 "2007-01-25T00:06:01Z")

</div>

yeah. thank you. i found validates\_associated :children a little while  
after posting here.  
pretty simple if you know it. 😉

thank you.
