diff options
author | Jan Varwig <jan@varwig.org> | 2011-11-27 11:47:45 +0100 |
---|---|---|
committer | Jan Varwig <jan@varwig.org> | 2011-11-27 22:29:56 +0100 |
commit | cba5a3a367f0f9d3be042c0782716fdfd4930f1c (patch) | |
tree | fc17fcfffd367e294e2f57818981094dc577ffe9 | |
parent | 3c81fc3b91ddfd4d115a7001640588eb93249261 (diff) | |
download | rails-cba5a3a367f0f9d3be042c0782716fdfd4930f1c.tar.gz rails-cba5a3a367f0f9d3be042c0782716fdfd4930f1c.tar.bz2 rails-cba5a3a367f0f9d3be042c0782716fdfd4930f1c.zip |
Test case and fix for rails/rails#3450
Asssigning a parent id to a belongs_to association actually updates the object that is validated when the association has :validates => true
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/autosave_association_test.rb | 11 |
2 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 056170d82a..d709a77fb0 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -264,7 +264,7 @@ module ActiveRecord # turned on for the association. def validate_single_association(reflection) association = association_instance_get(reflection.name) - record = association && association.target + record = association && association.reader association_valid?(reflection, record) if record end diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 4ad2cdfc7e..4c3f2bda57 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -347,6 +347,17 @@ class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::Test client.save! assert_no_queries { assert_equal apple, client.firm } end + + def test_validation_does_not_validate_stale_association_target + valid_developer = Developer.create!(:name => "Dude", :salary => 50_000) + invalid_developer = Developer.new() + + auditlog = AuditLog.new(:message => "foo") + auditlog.developer = invalid_developer + auditlog.developer_id = valid_developer.id + + assert auditlog.valid? + end end class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase |