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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2837408ba0375c40b235f0a5a5107d37?d=identicon&s=25 Doug Livesey (biot023)
on 2006-06-12 12: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
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.
This topic is locked and can not be replied to.