Rails 3.1 身份映射问题?

发布于 2024-11-27 18:58:24 字数 114 浏览 0 评论 0原文

有谁知道 Rails 3.1 IdentityMap 功能的关键问题导致默认情况下强制禁用该功能?我确信存在一些小具体问题,但是在为已构建的 Rails 3.1 应用程序启用它之前,是否有任何人应该注意的重大问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

定格我的天空 2024-12-04 18:58:24

代码中的注释

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

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.
柠檬色的秋千 2024-12-04 18:58:24

当您查看文档时,主要问题提出的问题是,身份映射中管理的对象尚无法处理关联,因此它现在还没有完全准备好用于现实世界。

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

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.

左秋 2024-12-04 18:58:24

我知道的两个小问题是:

  1. 如果您继承模型,并且想要从一种拼写错误的对象切换到另一种对象,那么首先您需要从身份映射中删除对象,然后创建一个新对象。示例:

    A类< ActiveRecord::基础
    结尾
    
    B级< ActiveRecord::基础
    结尾
    
    a = A.创建!
    a.update_attribute:类型,'B'
    b = B.查找a.id
    #=> #<答:...>
    ActiveRecord::IdentityMap.remove(a) 如果 ActiveRecord::IdentityMap.enabled?
    b = B.查找a.id
    #=> #
    
  2. 另一个小问题是恒等映射可能会与测试中的内容相符。因为它不会在每次测试后截断其存储库。为了做到这一点,需要将其添加到测试框架配置中。 Rspec 示例:

    RSpec.configure do |config|
      config.after:每个都做
        数据库清理器.clean
        ActiveRecord::IdentityMap.clear
      结尾
    结尾
    

我的意见是可以使用恒等映射,但部分使用。默认情况下为每个单个对象启用它是一个坏主意,但为特定模型启用它是一个好主意。假设您有一个语言表,这是相当静态的数据,或者可能按国家/地区划分。为什么不将它们全部加载到身份映射中。但是,对于动态数据(例如用户或不断变化的不同数据),无需将其存储在内存中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文