Sha256: 5dfb9a4725dd2f754f00dbc0fe0d89a22befc5d99c33cfc36b50c23e05116851

Contents?: true

Size: 1.3 KB

Versions: 8

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require "spec_helper"
require_relative './embeds_many_models'

describe Mongoid::Association::Embedded::EmbedsMany do

  context 'when projecting with #only' do
    before do
      congress = EmmCongress.new(name: 'foo')
      congress.legislators << EmmLegislator.new(a: 1, b: 2)
      congress.save!
    end

    let(:congress) do
      EmmCongress.where(name: 'foo').only(:name, 'legislators._id', 'legislators.a').first
    end

    let(:legislator) { congress.legislators.first }

    it 'populates specified fields only' do
      expect(legislator.a).to eq(1)
      # has a default value specified in the model
      expect do
        legislator.b
      end.to raise_error(ActiveModel::MissingAttributeError)
      expect(legislator.attributes.keys).to eq(['_id', 'a'])
    end

    context 'when using only with $' do
      before do
        Patient.destroy_all
        Patient.create!(
          title: 'Steve',
          addresses: [
            Address.new(number: '123'),
            Address.new(number: '456'),
          ],
        )
      end

      let(:patient) do
        Patient.where('addresses.number' => {'$gt' => 100}).only('addresses.$').first
      end

      it 'loads embedded association' do
        expect(patient.addresses.first.number).to eq(123)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mongoid-7.5.4 spec/mongoid/association/embedded/embeds_many_query_spec.rb
mongoid-7.5.3 spec/mongoid/association/embedded/embeds_many_query_spec.rb
mongoid-7.5.2 spec/mongoid/association/embedded/embeds_many_query_spec.rb
mongoid-7.5.1 spec/mongoid/association/embedded/embeds_many_query_spec.rb
mongoid-7.4.3 spec/mongoid/association/embedded/embeds_many_query_spec.rb
mongoid-7.5.0 spec/mongoid/association/embedded/embeds_many_query_spec.rb
mongoid-7.4.1 spec/mongoid/association/embedded/embeds_many_query_spec.rb
mongoid-7.4.0 spec/mongoid/association/embedded/embeds_many_query_spec.rb