Clarified intentions of validates_associated #870

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1602 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 a53372c2
......@@ -572,7 +572,10 @@ def validates_exclusion_of(*attr_names)
# validates_associated :book
# end
#
# this would specify a circular dependency and cause infinite recursion. The Rails team recommends against this practice.
# ...this would specify a circular dependency and cause infinite recursion.
#
# NOTE: This validation will not fail if the association hasn't been assigned. If you want to ensure that the association
# is both present and guaranteed to be valid, you also need to use validates_presence_of.
#
# Configuration options:
# * <tt>on</tt> Specifies when this validation is active (default is :save, other options :create, :update)
......
......@@ -784,4 +784,14 @@ def test_conditional_validation_using_block_false
assert t.valid?
assert !t.errors.on(:title)
end
end
def test_validates_associated_missing
Reply.validates_presence_of(:topic)
r = Reply.create("title" => "A reply", "content" => "with content!")
assert !r.valid?
assert r.errors.on(:topic)
r.topic = Topic.find :first
assert r.valid?
end
end
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册