Permalink
Browse files

Dont add duplicate 1-n docs while eager loading. Fixes #1987.

  • Loading branch information...
1 parent 357cba5 commit db472a6fda7d95d78d30da00523ecf811dd8e702 @durran durran committed May 12, 2012
Showing with 41 additions and 1 deletion.
  1. +3 −0 CHANGELOG.md
  2. +2 −1 lib/mongoid/identity_map.rb
  3. +5 −0 spec/app/models/blog.rb
  4. +1 −0 spec/app/models/post.rb
  5. +30 −0 spec/functional/mongoid/finders_spec.rb
View
@@ -10,6 +10,9 @@ For instructions on upgrading to newer versions, visit
* \#1994 `dependent: :delete` only hits the database once now for one to
many and many to many relations instead of once for each document.
+* \#1987 Don't double-insert documents into identity map when eager loading
+ twice inside the unit of work.
+
* \#1976 Don't execute eager load queries when base query is empty.
* \#1953 Uniqueness validation now works on localized fields.
@@ -62,7 +62,8 @@ def set(document)
#
# @since 2.2.0
def set_many(document, selector)
- (documents_for(document.class)[selector] ||= []).push(document)
+ documents = documents_for(document.class)[selector] ||= []
+ documents.push(document) unless documents.include?(document)
end
# Set a document in the identity map for the provided selector.
View
@@ -0,0 +1,5 @@
+class Blog
+ include Mongoid::Document
+ has_many :posts, :validate => false
+ default_scope includes(:posts)
+end
View
@@ -11,6 +11,7 @@ class Post
belongs_to :person
belongs_to :author, :foreign_key => :author_id, :class_name => "User"
+ belongs_to :blog
has_and_belongs_to_many :tags
has_many :videos, :validate => false
@@ -4,10 +4,40 @@
before do
Person.delete_all
+ Blog.delete_all
+ Post.delete_all
end
describe "#find" do
+ context "when eager loading in a default scope" do
+
+ before(:all) do
+ Mongoid.identity_map_enabled = true
+ end
+
+ after(:all) do
+ Mongoid.identity_map_enabled = false
+ end
+
+ let!(:blog) do
+ Blog.create
+ end
+
+ let!(:post_one) do
+ blog.posts.create(:title => "one")
+ end
+
+ let!(:post_two) do
+ blog.posts.create(:title => "two")
+ end
+
+ it "does not duplicate ids in the eager load query" do
+ Blog.where(_id: blog.id).first.posts.should have(2).entries
+ Blog.where(_id: blog.id).first.posts.should have(2).entries
+ end
+ end
+
context "when using integer ids" do
before(:all) do

0 comments on commit db472a6

Please sign in to comment.