Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

handle nil associations

  • Loading branch information...
commit 938854e5b48c18a9ec957cb3d22c83a6c7dfae1e 1 parent b6c6d07
@flyerhzm authored
View
5 lib/mongoid-eager-loading/mongoid/criterion/eager_loading.rb
@@ -92,7 +92,7 @@ def assign_associations(documents, reflection)
if key_value.is_a?(Array)
key_value.collect { |v| id_associations_map[v] }
else
- id_associations_map[key_value].first
+ id_associations_map[key_value] ? id_associations_map[key_value].first : nil
end
document.instance_variable_set("@#{reflection.name}", associations)
end
@@ -100,7 +100,8 @@ def assign_associations(documents, reflection)
end
def to_array(value)
- value.is_a?(Array) ? value : [value]
+ array = value.is_a?(Array) ? value : [value]
+ array.compact
end
def id_documents_map
View
19 spec/mongoid-eager-loading/mongoid/criterion/eager_loading_spec.rb
@@ -26,19 +26,23 @@
@person1 = Person.create(:title => "Sir", :age => 100, :aliases => ["D", "Durran"], :ssn => "666666666")
@person2 = Person.create(:title => "Madam", :age => 1, :ssn => "098-76-5434")
+ @person3 = Person.create(:title => "Sir", :age => 10, :aliases => ["R", "Richard"], :ssn => "111111111")
@game1 = @person1.create_game(:score => 10)
@game2 = @person2.create_game(:score => 20)
+ @game3 = Game.create(:score => 30)
@post1 = @person1.posts.create(:title => "post1")
@post2 = @person1.posts.create(:title => "post2")
@post3 = @person2.posts.create(:title => "post3")
@post4 = @person2.posts.create(:title => "post4")
+ @post5 = Post.create(:title => "post5")
@preference1 = @person1.preferences.create(:name => "preference1")
@preference2 = @person1.preferences.create(:name => "preference2")
@preference3 = @person2.preferences.create(:name => "preference3")
@preference4 = @person2.preferences.create(:name => "preference4")
+ @preference5 = Preference.create(:name => "preference5")
end
it "preload references_one association" do
@@ -52,13 +56,16 @@
id_documents_map = criteria.send(:id_documents_map)
id_documents_map[@person1.id].should == [@person1]
id_documents_map[@person2.id].should == [@person2]
+ id_documents_map[@person3.id].should == [@person3]
id_associations_map = criteria.send(:id_associations_map)
id_associations_map[@person1.id].should == [@game1]
id_associations_map[@person2.id].should == [@game2]
+ id_associations_map[@person3.id].should == nil
@person1.game.should == @game1
@person2.game.should == @game2
+ @person3.game.should == nil
end
it "preload references_many association" do
@@ -72,13 +79,16 @@
id_documents_map = criteria.send(:id_documents_map)
id_documents_map[@person1.id].should == [@person1]
id_documents_map[@person2.id].should == [@person2]
+ id_documents_map[@person3.id].should == [@person3]
id_associations_map = criteria.send(:id_associations_map)
id_associations_map[@person1.id].should == [@post1, @post2]
id_associations_map[@person2.id].should == [@post3, @post4]
+ id_associations_map[@person3.id].should == nil
@person1.posts.should == [@post1, @post2]
@person2.posts.should == [@post3, @post4]
+ @person3.posts.should == []
end
it "preload references_many_as_array association" do
@@ -94,15 +104,18 @@
id_documents_map[@preference2.id].should == [@person1]
id_documents_map[@preference3.id].should == [@person2]
id_documents_map[@preference4.id].should == [@person2]
+ id_documents_map[@preference5.id].should == nil
id_associations_map = criteria.send(:id_associations_map)
id_associations_map[@preference1.id].should == [@preference1]
id_associations_map[@preference2.id].should == [@preference2]
id_associations_map[@preference3.id].should == [@preference3]
id_associations_map[@preference4.id].should == [@preference4]
+ id_associations_map[@preference5.id].should == nil
@person1.preferences.should == [@preference1, @preference2]
@person2.preferences.should == [@preference3, @preference4]
+ @person3.preferences.should == []
end
context "referenced_in" do
@@ -117,13 +130,16 @@
id_documents_map = criteria.send(:id_documents_map)
id_documents_map[@person1.id].should == [@game1]
id_documents_map[@person2.id].should == [@game2]
+ id_documents_map[@person3.id].should == nil
id_associations_map = criteria.send(:id_associations_map)
id_associations_map[@person1.id].should == [@person1]
id_associations_map[@person2.id].should == [@person2]
+ id_associations_map[@person3.id].should == nil
@game1.person.should == @person1
@game2.person.should == @person2
+ @game3.person.should == nil
end
it "preload referenced_in association to references_many" do
@@ -137,15 +153,18 @@
id_documents_map = criteria.send(:id_documents_map)
id_documents_map[@person1.id].should == [@post1, @post2]
id_documents_map[@person2.id].should == [@post3, @post4]
+ id_documents_map[@person3.id].should == nil
id_associations_map = criteria.send(:id_associations_map)
id_associations_map[@person1.id].should == [@person1]
id_associations_map[@person2.id].should == [@person2]
+ id_associations_map[@person3.id].should == nil
@post1.person.should == @person1
@post2.person.should == @person1
@post3.person.should == @person2
@post4.person.should == @person2
+ @post5.person.should == nil
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.