Rails 3.1 Identity Map问题?

有谁知道Rails 3.1 IdentityMapfunction的主要问题是强制默认禁用该function? 我确信存在一些小问题,但在为已经构建的Rails 3.1应用程序启用它之前,是否有任何人应该注意的重大问题?

当你查看文档时 ,提出的主要问题是在身份映射中管理的对象还无法处理关联,所以它现在还没有为现实世界的使用做好准备。

该文档明确指出该function仍在开发中,因此没有人应该真正在野外使用它。

从代码中的注释 :

# 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. 

我知道的两个小问题是:

  1. 如果inheritance模型,并且想要从一个对象的错误切换到另一个,则首先需要从身份映射中删除对象,然后创建一个新对象。 例:

     class A < ActiveRecord::Base end class B < ActiveRecord::Base end a = A.create! a.update_attribute :type, 'B' b = B.find a.id #=> # ActiveRecord::IdentityMap.remove(a) if ActiveRecord::IdentityMap.enabled? b = B.find a.id #=> # 
  2. 另一个小问题是身份地图可能会在测试中发现问题。 因为它不会在每次测试后截断其存储库。 要做到这一点,需要将其添加到测试框架配置中。 Rspec示例:

     RSpec.configure do |config| config.after :each do DatabaseCleaner.clean ActiveRecord::IdentityMap.clear end end 

我的观点是可以使用身份地图,但部分使用。 默认情况下为每个单个对象启用它是一个坏主意,但将它启用到特定模型是个好主意。 比如,你有一个语言表,这是非常静态的数据,或者可能是国家。 为什么不将它们全部加载到身份地图中。 但是,对于动态数据(如用户或不同的东西,不断变化),不需要将其存储在内存中。

Interesting Posts