# Validating Child Objects against Parent

**URL:** <https://discuss.rubyonrails.org/t/validating-child-objects-against-parent/2780>\
**Category:** rubyonrails-talk\
**Created:** [October 8, 2006, 1:30pm UTC](https://discuss.rubyonrails.org/t/validating-child-objects-against-parent/2780 "2006-10-08T13:30:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_James](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mark\_James](https://discuss.rubyonrails.org/u/Mark_James)\
**Post date:** [October 8, 2006, 1:30pm UTC](https://discuss.rubyonrails.org/t/validating-child-objects-against-parent/2780/1 "2006-10-08T13:30:15Z")

</div>

Ajaya Agrawalla wrote:

> This is my situation. I have a form where I create a Parent Object and Two Child object. This is all in one form. In my Controller create.. I need to do validation on all three(parent and 2 child).
> 
> But my child object needs validate a attribute against a attribute of the parent. To summarize..
> 
> Parent{:age =\> 50} child {:age =\> 10}
> 
> In child validation a like to say def validate &nbsp;&nbsp;if self.age \> self.parent.age &nbsp;&nbsp;&nbsp;&nbsp;# errror &nbsp;&nbsp;end end
> 
> But the problem is the parent is new and not in the database yet. So the question is how can I pass a object to the child's validation. I would like to say something like child.valid?(parent)

Just set child.parent to the parent object, even if the parent object is new. This is normally done in the controller as

&nbsp;&nbsp;&nbsp;@child = @parent.build\_child(params[:child])

which is equivalent to

&nbsp;&nbsp;&nbsp;@child = Child.new(params[:child]) &nbsp;&nbsp;&nbsp;@child.parent = @parent

---

<div class="post-metadata">

**Author:** ![ajaya](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/ajaya/32/790_2.png) [@ajaya](https://discuss.rubyonrails.org/u/ajaya)\
**Post date:** [October 8, 2006, 6:33pm UTC](https://discuss.rubyonrails.org/t/validating-child-objects-against-parent/2780/2 "2006-10-08T18:33:28Z")

</div>

Well.. Seems like this doesn't work.

@p = Parent.new(params[:parent]) @c = @p.childs.build(params[:child])

@c.parent is set to nil and I see that @c.parent\_id is set 1 even id @p is new.

The @p.build\_child method not there..

Any idea what I am doing wrong?

AJ

---

<div class="post-metadata">

**Author:** ![Mark\_James](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mark\_James](https://discuss.rubyonrails.org/u/Mark_James)\
**Post date:** [October 8, 2006, 7:08pm UTC](https://discuss.rubyonrails.org/t/validating-child-objects-against-parent/2780/3 "2006-10-08T19:08:23Z")

</div>

AJAY A wrote:

> Well.. Seems like this doesn't work.
> 
> @p = Parent.new(params[:parent]) @c = @p.childs.build(params[:child])
> 
> @c.parent is set to nil and I see that @c.parent\_id is set 1 even id @p is new.

Is params[:child][:parent\_id] set?

In any case, looking at the core source I see that build does not in fact assign a new parent. You have to use the explicit method I posted:

@child = Child.new(params[:child]) @child.parent = @parent

> The @p.build\_child method not there..

Yes, that's for has\_one. You have a has\_many.

---

<div class="post-metadata">

**Author:** ![ajaya](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/ajaya/32/790_2.png) [@ajaya](https://discuss.rubyonrails.org/u/ajaya)\
**Post date:** [October 8, 2006, 7:14pm UTC](https://discuss.rubyonrails.org/t/validating-child-objects-against-parent/2780/4 "2006-10-08T19:14:00Z")

</div>

params[:child][:parent\_id] is not known until the record is saved.

Anyway.. I would use the explicit method.

Thanks for the help

AJ
