Closed dallonf closed 13 years ago
Turn off auto_validation on the relationship.
Related, I typically use this monkey-patch (which I don't think will fix your problem exactly but will fix similar problems):
module DataMapper
module Validations
module AutoValidations
# Always skip auto-validations on primary keys, it turns nested saves into a whole world of pain.
def skip_auto_validation_for?(property)
(property.key? && !property.options[:auto_validation]) ||
(property.options.key?(:auto_validation) && !property.options[:auto_validation])
end
end
end
end
Say I have a Topic object which has_many :posts, and when I create a Topic, I want to also include an opening Post.
If I assigned a Topic to the Post (or vice versa) without first saving the Topic, post.valid? will return false and post.errors will include "Topic must not be blank". I believe that since the topic relationship is set, post.valid? should return true. (although if the Topic is not saved first, post.save should fail)
A workaround is to use topic.save and handle a false result from that, but I personally prefer to use the valid? methods before saving.