title: why does calling `length` on an ActiveRecord relation change the size?
body: <p>On Rails 4.2.10 I have this model:</p>
  
  <pre><code>class RegistrationApproval &lt; ActiveRecord::Base
    belongs_to :registering_user, class_name: 'User', inverse_of: :registration_approvals
    belongs_to :approver, class_name: 'User', inverse_of: :approved_registrations
  end
  </code></pre>
  
  <p>And in my <code>User</code> model I have this:</p>
  
  <pre><code>has_many :registration_approvals, foreign_key: 'registering_user_id', dependent: :destroy, inverse_of: :registering_user
  has_many :registration_approvers, through: :registration_approvals, class_name: 'User', source: :approver, inverse_of: :registering_users
  has_many :approved_registrations, class_name: 'RegistrationApproval', foreign_key: 'approver_id', dependent: :destroy, inverse_of: :approver
  has_many :registering_users, through: :approved_registrations, class_name: 'User', inverse_of: :registration_approvers
  
  def approve_registration(approver)
    registration_approvals.create(approver: approver)
    Rails.logger.debug "approvers: #{registration_approvers.length}"
    return [registration_approvers.size, registration_approvers.count]
  end
  </code></pre>
  
  <p>In the the <code>approve_registration</code> method the debug statement changes the output of that method. Without the debug statement the when there are two approvals for the user <code>registration_approvers.size</code> evaluates to <code>2</code> as you would expect, but with the debug statement it evaluates to <code>1</code>. Why does this happen? Is this a bug in Rails 4.2?</p>
  
  <p>The <code>count</code> evaluation remains unaffected.</p>
  
  <p>It seems to be the call to <code>registration_approvers.length</code> that causes the change in <code>size</code></p>
  
  <p>Here is a failing test I have that shows this problem:</p>
  
  <pre><code> it 'should have size and count equal' do
      registering_user = FactoryBot.create(:user)
      _(registering_user).must_be :persisted?
      approver1 = FactoryBot.create(:user)
      _(approver1).must_be :persisted?
      approver2 = FactoryBot.create(:user)
      _(approver2).must_be :persisted?
      registering_user.approve_registration(approver1)
      size, count = registering_user.approve_registration(approver2)
      _(count).must_equal 2
      _(size).must_equal 2
    end
  </code></pre>
  
  <p>All the asserts in the test pass except the one in the last line that fails because <code>size</code> is <code>1</code>. The test passes when the call to <code>registration_approvers.length</code> is removed from the <code>approve_registration</code> method.</p>
  
  <p>This is a problem for me since in the <code>approve_registration</code> method I want to iterate through the <code>registration_approvers</code> with <code>each</code> but when I do that it misses the last in the collection, presumably because the <code>each</code> method calls <code>length</code> on the collection and changes the value of size and then quits the loop early thinking it's reached the end.</p>
  
  <p>If I can get a workaround way of iterating through that collection without having <code>length</code> called on it, that would help a bit.</p>