aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xactiverecord/lib/active_record/validations.rb5
-rwxr-xr-xactiverecord/test/validations_test.rb12
2 files changed, 15 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index f455b97e0a..fb207ef4b2 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -572,7 +572,10 @@ module ActiveRecord
# 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)
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb
index 576d1f6c3d..8a918e2255 100755
--- a/activerecord/test/validations_test.rb
+++ b/activerecord/test/validations_test.rb
@@ -784,4 +784,14 @@ class ValidationsTest < Test::Unit::TestCase
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