According to http://dev.rubyonrails.org/ticket/4147,
validates_presence_of the foreign key is the proper way to validate
the presence associated objects. However, the documentation warning
regarding this was later removed.
So, question is: How do I validate the presence of associated objects,
while still allowing the building of associated objects to an unsaved
model?
According to http://dev.rubyonrails.org/ticket/4147,
validates_presence_of the foreign key is the proper way to validate
the presence associated objects. However, the documentation warning
regarding this was later removed.
So, question is: How do I validate the presence of associated objects,
while still allowing the building of associated objects to an unsaved
model?
Christian, this comes up quite often. I'm glad the incorrect
warning has been removed from the API, but I would dispute the
validity of that old ticket [the blog post explaining the
ticket-creator's reasoning is no longer available].
Problem is that setting the (unsaved) owner object on an associated
object causes a lot of tests to break. Namely, validations go into a
recursion of e.g. validate_associated_records_for_developer and
validate_associated_records_for_audit_logs back and forth ad
infinitum.
Problem is that setting the (unsaved) owner object on an associated
object causes a lot of tests to break. Namely, validations go into a
recursion of e.g. validate_associated_records_for_developer and
validate_associated_records_for_audit_logs back and forth ad
infinitum.
I guess it's the tests that require fixing. And AR should be
detecting loopy validations and raising an error.