Sha256: dfbb53d370118b10ec1bda697cbecff0a8e95ac52af8e2a5281a74372947564e

Contents?: true

Size: 1.12 KB

Versions: 23

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

require "spec_helper"
require_relative './has_many_models'

describe Mongoid::Association::Referenced::BelongsTo do
  context 'when projecting with #only' do
    before do
      school = HmmSchool.create!(district: 'foo', team: 'Bulldogs')
      HmmStudent.create!(school: school, name: 'Dave', grade: 10)
    end

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

    let(:school) { student.school }

    it 'populates specified fields only' do
      pending 'https://jira.mongodb.org/browse/MONGOID-4704'

      expect(school.district).to eq('foo')
      # has a default value specified in the model
      expect do
        school.team
      end.to raise_error(ActiveModel::MissingAttributeError)
      expect(student.attributes.keys).to eq(['_id', 'name'])
    end

    # Delete this test when https://jira.mongodb.org/browse/MONGOID-4704 is
    # implemented and above test is unpended
    it 'fetches all fields' do
      expect(school.district).to eq('foo')
      expect(school.team).to eq('Bulldogs')
    end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
mongoid-7.1.11 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.3.2 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.2.5 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.1.10 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.1.9 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.2.4 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.3.1 spec/mongoid/association/referenced/belongs_to_query_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.3.0 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.2.3 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.1.8 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.2.2 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.2.1 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.1.7 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.2.0 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.1.6 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.1.5 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.2.0.rc1 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.1.4 spec/mongoid/association/referenced/belongs_to_query_spec.rb
mongoid-7.1.2 spec/mongoid/association/referenced/belongs_to_query_spec.rb