Has_many and validates_associated

By trial and error I seem to arrive to the conclusion that

1. Validation recurses through has_many by default and
   a root save! call raises an excepcion if some child no
   matter how nested is fails validation

2. Validation does not recurse through has_one by default
   and you can change this with validates_associated, in
   which case exceptions behave like above

Besides not being symmetric, the documentation of
validates_associated has an example where models associated with
has_many are passed to the method, so that makes me wonder whether 1
and 2 above are actually wrong.

– fxn

Xavier N. wrote:

Besides not being symmetric, the documentation of validates_associated
has an example where models associated with has_many are passed to the
method, so that makes me wonder whether 1 and 2 above are actually wrong.

Yes, this automatic validation check for new_records in has_many
associations surprised me, and seems to be undocumented. It comes
via this automatically-added callback:

http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/associations.rb#L778

Is there a good reason for doing this rather than letting the user
control it through a validates_associated declaration?

By the way, there is a bug in validates_associated for has_many
associations whereby the validation will stop when the first
nil or invalid object in the collection is encountered. Instead,
all objects should be checked for validity so that their errors
can be flagged in the view. See:

http://dev.rubyonrails.org/ticket/4276


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