Validates_associated -- explicitly setting collection?


#1

Hi, I have a class (say Parent) that has many of two other classes, say
FirstChild and SecondChild. These could look like this:

class Parent < ActiveRecord::Base
has_many :first_children, :dependent => true
has_many :first_children, :dependent => true
validates_associated :first_children
validates_associated :second_children
end

class FirstChild < ActiveRecord::Base
belongs_to :parent
validates_format_of :birth_date, :with => /^\d{4}/\d{2}/\d{2}$/
end

class SecondChild < ActiveRecord::Base
belongs_to :parent
validates_format_of :birth_date, :with => /^\d{4}/\d{2}/\d{2}$/
end

The key bit here is the validation – namely the validates_associated.
If I collect hashes of data from a form for the child classes, I should
surely be able to assign them to the Parent class, and then have that
fail validation if one of them fails?
Maybe like:

parent = Parent.find(params[:id])
first_children = FirstChild.update(
params[:first_child].keys, params[:first_child].values)
second_children = SecondChild.update(
params[:second_child].keys, params[:second_child].values)
parent.first_children = first_children
parent.second_children = second_children

if I debug here, none of the errors in the local arrays first_children

or

second_children are in available in either parent.first_children or

parent.second_children

if parent.update_attributes(params[:parent])

whatever…

else

should end up here if any errors in parent or child collections

end

(I’ve also tried a number of other ways to assign the collections,
including ActiveRecord::Base.update_attribute(), but none of them work.)

Can anyone help me to accomplish what I need to do here?
If this can’t be made to work, them I don’t really see any point to
validates_associated.
Any help very gratefully received by a very tired programmer!
Cheers,
doug.