Forum: Ruby on Rails validates_associated -- explicitly setting collection?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Doug L. (Guest)
on 2006-06-12 14:43
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

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

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

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
# second_children are in available in either parent.first_children or
# parent.second_children
if parent.update_attributes(params[:parent])
  # whatever...
  # should end up here if any errors in parent or child collections

(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
Any help *very* gratefully received by a *very* tired programmer!
This topic is locked and can not be replied to.