title: Why is this model's association disappearing from the test once the assertion is made (after a rails 5.1 > 5.2 upgrade)?
body: <p>I have a model in this application that tracks role changes. The test I'm about to describe has been passing for the last couple years no problem and only starting failing as I upgraded the rails version from 5.1 to 5.2.</p>
  
  <p>Here's an excerpt from the model:</p>
  
  <pre><code>class RoleChange &lt; ApplicationRecord
    acts_as_paranoid
    belongs_to :actor, class_name: 'User', foreign_key: 'actor_id'
    belongs_to :subject, class_name: 'User', foreign_key: 'subject_id'
    validates :subject_id, presence: true
  
    def subject
      User.with_deleted.find(subject_id)
    end
  
    ...
  end
  </code></pre>
  
  <p>And the assertion that is failing in the spec is the following</p>
  
  <pre><code>require 'rails_helper'
  
  RSpec.describe RoleChange, type: :model do
    let(:organization) { create(:organization) }
    let(:admin) { organization.users.admins.first }
    let!(:user) { create(:employee_user, organization: organization) }
    subject { create(:role_change, subject: user, actor: admin) }
  
    describe 'associations' do
      it { is_expected.to belong_to(:actor) }
      it { is_expected.to belong_to(:subject) }
    end
  
    ...
  
  end
  </code></pre>
  
  <p>The second association assertion fails with the following error:</p>
  
  <pre><code>  1) RoleChange associations is expected to belong to subject required:
       Failure/Error: User.with_deleted.find(subject_id)
  
       ActiveRecord::RecordNotFound:
         Couldn't find User without an ID
       # ./app/models/role_change.rb:28:in `subject'
  </code></pre>
  
  <p>This is very frustrating.</p>
  
  <p>When I <code>binding.pry</code> inside of an <code>it</code> block, the subject appears to be <code>valid?</code> and <code>persisted?</code> with a <code>subject_id</code> as you'd expect.</p>
  
  <p>It's only once I run the assertion, the <code>subject_id</code> magically becomes <code>nil</code>.</p>
  
  <p>For additional context, my adventures in the rails console:</p>
  
  <p><a href="https://i.stack.imgur.com/vkMq9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vkMq9.png" alt="The subject is there!"></a></p>
  
  <p>when I run the assertion, it fails and says that nil can’t be found:</p>
  
  <p><a href="https://i.stack.imgur.com/9de8U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9de8U.png" alt="Schrodinger&#39;s foreign key"></a></p>
  
  <p>Totally baffling. Can you help me finish this rails upgrade? It's my last failing test.</p>
  
  <p>UPDATE:</p>
  
  <p>deleting the <code>subject</code> getter method from <code>RoleChange</code> makes the test pass but then I lose the benefit of including deleted users from the query. So... doesn't actually seem like a solution.</p>