Does anyone know the key issues that the Rails 3.1 IdentityMap feature has that has forced the feature to be disabled by default? I'm sure that there are minor specific issues, but are there any major issues that anyone should be aware of before enabling it for an already built Rails 3.1 application?
From the comments in the code:
# 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 accidently removed.
#
# This inconsistency is meant to be fixed in future Rails releases.
When you look at the documentation the main issue raised is that objects managed in the Identity Map can not handle associations yet, so it's not quite ready for real world usage right now.
The documentation states clearly that the feature is still under development, so no one should really be using it in the wild.
Two minor problems which I aware of are:
If you inheriting models, and you want to switch from one typo of object to another, then first you need delete your object from identity map, and after that create a new object. Example:
class A < ActiveRecord::Base
end
class B < ActiveRecord::Base
end
a = A.create!
a.update_attribute :type, 'B'
b = B.find a.id
#=> #<A:...>
ActiveRecord::IdentityMap.remove(a) if ActiveRecord::IdentityMap.enabled?
b = B.find a.id
#=> #<B:...>
Another small issue is that the identity map may mees up the thing in the tests. As it do not truncates its repository after each test. To make it to do so one needs to add that into test framework configurations. Rspec example:
RSpec.configure do |config|
config.after :each do
DatabaseCleaner.clean
ActiveRecord::IdentityMap.clear
end
end
My opinion is that identity map may be used, but partially. It is a bad idea to enable it by default for each single object, but it will be a good idea to enable it to specific models. Say, you have a table of languages, which is pretty static data, or may by countries. Why not to load them all in to identity map. But, with dynamic data (like users, or something different, that constantly changes), there is no need to store that in the memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With