Permalink
Browse files

Document identity map inconsistency with associations, closes #474.

  • Loading branch information...
josevalim committed May 10, 2011
1 parent 5fc3564 commit 302c912bf6bcd0fa200d964ec2dc4a44abe328a6
Showing with 28 additions and 1 deletion.
  1. +28 −1 activerecord/lib/active_record/identity_map.rb
@@ -12,7 +12,34 @@ module ActiveRecord
# In order to enable IdentityMap, set <tt>config.active_record.identity_map = true</tt>
# in your <tt>config/application.rb</tt> file.
#
# IdentityMap is disabled by default.
# IdentityMap is disabled by default and still in development (i.e. use it with care).
#
# == Associations
#
# Active Record Identity Map does not track associations yet. For example:
#
# comment = @post.comments.first
# comment.post = nil
# @post.comments.include?(comment) #=> true
#
# Ideally, the example above would return false, removing the comment object from the
# post association when the association is nullified. This may cause side effects, as
# in the situation below, if Identity Map is enabled:
#
# Post.has_many :comments, :dependent => :destroy
#
# comment = @post.comments.first
# comment.post = nil
# comment.save
# Post.destroy(@post.id)
#
# Without using Identity Map, the code above will destroy the @post object leaving
# the comment object intact. However, once we enable Identity Map, the post loaded
# by Post.destroy is exactly the same object as the object @post. As the object @post
# still has the comment object in @post.comments, once Identity Map is enabled, the
# comment object will be acciddently removed.

This comment has been minimized.

@pmarreck

pmarreck Sep 24, 2012

Why is it wrong to remove the comment object when the association to comments has dependent: :destroy on it? Wouldn't that be what one would expect? Not sure why that is a problem...

This comment has been minimized.

@pokka

pokka Dec 10, 2012

Its problem is comment.post = nil,not Post.has_many :comments, :dependent => :destroy.

While comment.post = nil called,there have not any association between @post and comment,reasonable when @post deleted,comment should not be implicated.

Active Record Identity Map does not track associations,so the problem arise.

#
# This incosistency is meant to be fixed in future Rails releases.
#
module IdentityMap

0 comments on commit 302c912

Please sign in to comment.