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-5149 Skip inverse detection when inverse_of: nil is set #5027

Merged
merged 2 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/mongoid/association/relatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def bindable?(doc); false; end
# @since 7.0
def inverses(other = nil)
return [ inverse_of ] if inverse_of
return [] if @options.key?(:inverse_of) && !inverse_of

if polymorphic?
polymorphic_inverses(other)
else
Expand Down
18 changes: 16 additions & 2 deletions spec/mongoid/association/referenced/belongs_to_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,28 @@
describe Mongoid::Association::Referenced::BelongsTo do
context 'when projecting with #only' do
before do
academy1 = HmmAcademy.create!(name: 'Test Academy 1')
academy2 = HmmAcademy.create!(name: 'Test Academy 2')

school = HmmSchool.create!(district: 'foo', team: 'Bulldogs')
HmmStudent.create!(school: school, name: 'Dave', grade: 10)

HmmStudent.create!(
school: school,
name: 'Dave',
grade: 10,
current_academy: academy1,
previous_academy: academy2)
end

let(:student) do
HmmStudent.where(name: 'Dave').only(:school_id, 'school._id', 'school.district').first
HmmStudent
.where(name: 'Dave')
.only(:school_id, :previous_academy_id, 'previous_academy.name', 'school._id', 'school.district')
.first
end

let(:school) { student.school }
let(:previous_academy) { student.previous_academy }

it 'populates specified fields only' do
pending 'https://jira.mongodb.org/browse/MONGOID-4704'
Expand All @@ -33,6 +46,7 @@
it 'fetches all fields' do
expect(school.district).to eq('foo')
expect(school.team).to eq('Bulldogs')
expect(previous_academy.name).to eq('Test Academy 2')
end
end
end
11 changes: 11 additions & 0 deletions spec/mongoid/association/referenced/has_many_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class HmmAddress
belongs_to :company, class_name: 'HmmCompany'
end

class HmmAcademy
include Mongoid::Document

has_many :students, class_name: 'HmmStudent', inverse_of: :current_academy

field :name, type: String
end

class HmmSchool
include Mongoid::Document

Expand All @@ -36,6 +44,9 @@ class HmmSchool
class HmmStudent
include Mongoid::Document

belongs_to :current_academy, class_name: 'HmmAcademy', inverse_of: :students, optional: true
belongs_to :previous_academy, class_name: 'HmmAcademy', inverse_of: nil, optional: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should also go into a new model, unless there is something else from HmmStudent that your test would need.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can create a dedicated model for this yes.


belongs_to :school, class_name: 'HmmSchool'

field :name, type: String
Expand Down