Skip to content

Commit

Permalink
Merge pull request mongodb#3533 from rodrigosaito/include_id_in_only
Browse files Browse the repository at this point in the history
Change in behavior with MongoDB projections and Mongoid 'only' method.
  • Loading branch information
arthurnn committed Feb 28, 2014
2 parents c8af8d0 + 77e5875 commit bde8ac1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/mongoid/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def write_attributes(attrs = nil)
alias :attributes= :write_attributes

# Determine if the attribute is missing from the document, due to loading
# it from the database with missing fields.
# it from the database with missing fields, _id is always loaded
#
# @example Is the attribute missing?
# document.attribute_missing?("test")
Expand All @@ -237,6 +237,7 @@ def write_attributes(attrs = nil)
def attribute_missing?(name)
selection = __selected_fields
return false unless selection
selection.merge!({ "_id" => 1 })
(selection.values.first == 0 && selection[name.to_s] == 0) ||
(selection.values.first == 1 && !selection.has_key?(name.to_s))
end
Expand Down
4 changes: 4 additions & 0 deletions spec/mongoid/criteria_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2640,6 +2640,10 @@ class D
it "does not add _type to the fields" do
expect(criteria.options[:fields]["_type"]).to be_nil
end

it "always contains the id field" do
expect(criteria.first.id).to_not be_nil
end
end

context "when instantiating a class of another type inside the iteration" do
Expand Down

0 comments on commit bde8ac1

Please sign in to comment.