Index: test/associations_test.rb
===================================================================
--- test/associations_test.rb	(revision 4721)
+++ test/associations_test.rb	(working copy)
@@ -313,6 +313,13 @@
     firm.account = Account.find(:first).clone
     assert_queries(2) { firm.save! }
   end
+  
+  def test_save_when_nil_association_cached
+    firm = companies(:another_firm)
+    
+    assert_nil firm.account
+    assert_nothing_raised { firm.save! }
+  end
 end
 
 
Index: lib/active_record/associations.rb
===================================================================
--- lib/active_record/associations.rb	(revision 4721)
+++ lib/active_record/associations.rb	(working copy)
@@ -584,7 +584,7 @@
         module_eval do
           after_save <<-EOF
             association = instance_variable_get("@#{reflection.name}")
-            if !association.nil? && (new_record? || association.new_record? || association["#{reflection.primary_key_name}"] != id)
+            if association && association.target && (new_record? || association.new_record? || association["#{reflection.primary_key_name}"] != id)
               association["#{reflection.primary_key_name}"] = id
               association.save(true)
             end
Index: CHANGELOG
===================================================================
--- CHANGELOG	(revision 4721)
+++ CHANGELOG	(working copy)
@@ -1,5 +1,7 @@
 *SVN*
 
+* Fix caching of nil has_one associations. Closes #5767. [Jonathan Viney]
+
 * Cache nil results for has_one associations so multiple calls don't call the database.  Closes #5757. [Michael A. Schoen]
 
 * Add documentation for how to disable timestamps on a per model basis. Closes #5684. [matt@mattmargolis.net Marcel Molina Jr.] 
