Rails 3.1 IdentityMap機能がデフォルトで機能を無効にすることを余儀なくされている重要な問題を知っている人はいますか?マイナーな特定の問題があると確信していますが、すでにビルドされているRails 3.1アプリケーションで有効にする前に、誰かが知っておくべき大きな問題はありますか?
3 に答える
コード内のコメントから:
# 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.
ドキュメントを見ると、提起された主な問題は、IDマップで管理されているオブジェクトがまだ関連付けを処理できないため、現在、実際に使用する準備が整っていないことです。
ドキュメントには、この機能はまだ開発中であることが明確に記載されているため、実際に実際に使用することはできません。
私が知っている2つの小さな問題は次のとおりです。
モデルを継承していて、オブジェクトのタイプミスを別のタイプに切り替えたい場合は、最初にIDマップからオブジェクトを削除し、その後、新しいオブジェクトを作成する必要があります。例:
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:...>
もう1つの小さな問題は、恒等写像がテストの内容を台無しにする可能性があることです。各テストの後にリポジトリを切り捨てないため。そのためには、それをテストフレームワーク構成に追加する必要があります。Rspecの例:
RSpec.configure do |config| config.after :each do DatabaseCleaner.clean ActiveRecord::IdentityMap.clear end end
私の意見では、恒等写像を使用することもできますが、部分的に使用することもできます。単一のオブジェクトごとにデフォルトで有効にすることはお勧めできませんが、特定のモデルで有効にすることをお勧めします。たとえば、言語のテーブルがあります。これはかなり静的なデータであるか、国ごとの場合があります。それらすべてをIDマップにロードしてみませんか。ただし、動的データ(ユーザーなど、絶えず変化するもの)を使用する場合は、それをメモリに保存する必要はありません。