Sha256: 5449e3c61cd97ef63329b2b22d757e64748704ccc1060b1f8e338a04d2d61beb

Contents?: true

Size: 1.56 KB

Versions: 7

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

require 'spec_helper'

describe 'has_one associations' do
  context 'destroying parent in transaction with dependent child' do
    require_transaction_support

    let(:person) { Person.create! }
    let(:game) { Game.create!(person: person) }

    before do
      Person.delete_all
      Game.delete_all

      game
    end

    it 'works' do
      Person.count.should == 1
      Game.count.should == 1

      Person.with_session do |session|
        session.with_transaction do
          person.destroy
        end
      end

      Person.count.should == 0
      Game.count.should == 0
    end
  end

  context 're-associating the same object' do
    context 'with dependent: destroy' do
      let(:person) do
        Person.create!
      end

      let!(:game) do
        Game.create!(person: person) do
          person.reload
        end
      end

      it 'does not destroy the dependent object' do
        person.game.should == game
        person.game = person.game
        person.save!
        person.reload
        person.game.should == game
      end
    end

    context 'without dependent: destroy' do
      let(:person) do
        Person.create!
      end

      let!(:account) do
        Account.create!(person: person, name: 'foo').tap do
          person.reload
        end
      end

      it 'does not destroy the dependent object' do
        person.account.should == account
        person.account = person.account
        person.save!
        person.reload
        person.account.should == account
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
mongoid-7.1.11 spec/integration/associations/has_one_spec.rb
mongoid-7.1.10 spec/integration/associations/has_one_spec.rb
mongoid-7.1.9 spec/integration/associations/has_one_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/spec/integration/associations/has_one_spec.rb
mongoid-7.1.8 spec/integration/associations/has_one_spec.rb
mongoid-7.1.7 spec/integration/associations/has_one_spec.rb
mongoid-7.1.6 spec/integration/associations/has_one_spec.rb