Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 Identity Map issues?

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?

like image 599
matsko Avatar asked Aug 02 '11 00:08

matsko


3 Answers

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.
like image 105
Mori Avatar answered Nov 16 '22 05:11

Mori


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.

like image 22
Maurício Linhares Avatar answered Nov 16 '22 05:11

Maurício Linhares


Two minor problems which I aware of are:

  1. 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:...>
    
  2. 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.

like image 3
Max Avatar answered Nov 16 '22 05:11

Max