Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MONGOID-5797 Fix accessing parent when projected #5847

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/mongoid/association/accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ def get_relation(name, association, object, reload = false)
# during binding or when cascading callbacks. Whenever we retrieve
# associations within the codebase, we use without_autobuild.
if !without_autobuild? && association.embedded? && attribute_missing?(field_name)
raise Mongoid::Errors::AttributeNotLoaded.new(self.class, field_name)
# We always allow accessing the parent document of an embedded one.
try_get_parent = association.is_a?(
Mongoid::Association::Embedded::EmbeddedIn
) && field_name == association.key
raise Mongoid::Errors::AttributeNotLoaded.new(self.class, field_name) unless try_get_parent
end

if !reload && (value = ivar(name)) != false
Expand Down
4 changes: 4 additions & 0 deletions spec/mongoid/association/embedded/embeds_many_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
expect(legislator.attributes.keys).to eq(['_id', 'a'])
end

it 'allows accessing the parent' do
expect { legislator.congress }.not_to raise_error
end

context 'when using only with $' do
before do
Patient.destroy_all
Expand Down
Loading