Permalink
Browse files

Dont eager load when base query is blank.

  • Loading branch information...
1 parent 154af57 commit deeadf06a6aa5b9a6decf8781899b1faed5751e8 @durran durran committed May 6, 2012
Showing with 33 additions and 6 deletions.
  1. +2 −0 CHANGELOG.md
  2. +8 −6 lib/mongoid/contexts/mongo.rb
  3. +23 −0 spec/functional/mongoid/contexts/mongo_spec.rb
View
@@ -7,6 +7,8 @@ For instructions on upgrading to newer versions, visit
### Resolved Issues
+* \#1976 Don't execute eager load queries when base query is empty.
+
* \#1953 Uniqueness validation now works on localized fields.
* \#1936 Allow setting n levels deep embedded documents atomically without
@@ -144,13 +144,15 @@ def distinct(field)
#
# @since 2.4.1
def eager_load(docs)
- parent_ids = docs.map(&:id)
criteria.inclusions.reject! do |metadata|
- if metadata.macro == :referenced_in
- child_ids = load_ids(metadata.foreign_key)
- metadata.eager_load(child_ids)
- else
- metadata.eager_load(parent_ids)
+ unless docs.empty?
+ parent_ids = docs.map(&:id)
+ if metadata.macro == :referenced_in
+ child_ids = load_ids(metadata.foreign_key)
+ metadata.eager_load(child_ids)
+ else
+ metadata.eager_load(parent_ids)
+ end
end
end
end
@@ -174,6 +174,29 @@
end
end
+ describe "#eager_load" do
+
+ let(:criteria) do
+ Person.includes(:game)
+ end
+
+ let(:context) do
+ described_class.new(criteria)
+ end
+
+ context "when no documents are returned" do
+
+ let(:game_metadata) do
+ Person.reflect_on_association(:game)
+ end
+
+ it "does not make any additional database queries" do
+ game_metadata.expects(:eager_load).never
+ context.send(:eager_load, [])
+ end
+ end
+ end
+
describe "#empty?" do
context "when no documents are in the collection" do

0 comments on commit deeadf0

Please sign in to comment.