# Validating Child Objects against Parent

**URL:** <https://www.ruby-forum.com/t/validating-child-objects-against-parent/71379>\
**Category:** Rails\
**Created:** [October 8, 2006, 1:14am UTC](https://www.ruby-forum.com/t/validating-child-objects-against-parent/71379 "2006-10-08T01:14:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@ajay](https://www.ruby-forum.com/u/ajay)\
**Post date:** [October 8, 2006, 1:14am UTC](https://www.ruby-forum.com/t/validating-child-objects-against-parent/71379/1 "2006-10-08T01:14:38Z")

</div>

Folks,

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  
if self.age \> self.parent.age  
# errror  
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)

I am using hack right now to have a method on the child object and doing  
that. Is there more of a standard way to pass a object rails validation  
methods?

AJ

---

<div class="post-metadata">

**Author:** ![ajay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@ajay](https://www.ruby-forum.com/u/ajay)\
**Post date:** [October 8, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/validating-child-objects-against-parent/71379/2 "2006-10-08T15:32:16Z")

</div>

Ajaya A. wrote:

> In child validation a like to say  
> def validate  
> if self.age \> self.parent.age  
> # errror  
> 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

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

which is equivalent to

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

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

---

<div class="post-metadata">

**Author:** ![ajay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@ajay](https://www.ruby-forum.com/u/ajay)\
**Post date:** [October 8, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/validating-child-objects-against-parent/71379/3 "2006-10-08T20:36:16Z")

</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:** ![ajay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@ajay](https://www.ruby-forum.com/u/ajay)\
**Post date:** [October 8, 2006, 9:18pm UTC](https://www.ruby-forum.com/t/validating-child-objects-against-parent/71379/4 "2006-10-08T21:18:19Z")

</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

---

<div class="post-metadata">

**Author:** ![ajay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@ajay](https://www.ruby-forum.com/u/ajay)\
**Post date:** [October 8, 2006, 9:10pm UTC](https://www.ruby-forum.com/t/validating-child-objects-against-parent/71379/5 "2006-10-08T21:10:19Z")

</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.

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