Forum: Ruby on Rails has_many and validates_associated

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.
Xavier N. (Guest)
on 2006-03-18 17:57
(Received via mailing list)
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
Mark Reginald J. (Guest)
on 2006-03-20 02:53
(Received via mailing list)
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/activerec...

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.
This topic is locked and can not be replied to.