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