From 5ee3663101f224a30ec95f6dab4b02a4a922eb2f Mon Sep 17 00:00:00 2001
From: Emilio Tagua <miloops@gmail.com>
Date: Wed, 20 Oct 2010 19:04:09 -0300
Subject: Remove associated objects from IM when clearing them from association
 cache.

---
 activerecord/lib/active_record/associations.rb | 6 ++++++
 1 file changed, 6 insertions(+)

(limited to 'activerecord/lib')

diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 08a4ebfd7e..12b0601e3c 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -117,6 +117,12 @@ module ActiveRecord
     # Clears out the association cache.
     def clear_association_cache #:nodoc:
       self.class.reflect_on_all_associations.to_a.each do |assoc|
+        if IdentityMap.enabled? && instance_variable_defined?("@#{assoc.name}")
+          targets = [*instance_variable_get("@#{assoc.name}")]
+          targets.map! { |t| t.respond_to?(:target) ? t.target : t }
+          targets.compact!
+          targets.each { |r| IdentityMap.remove r }
+        end
         instance_variable_set "@#{assoc.name}", nil
       end if self.persisted?
     end
-- 
cgit v1.2.3