title: How to Determine if Rails Association is Eager Loaded?
body: <p>Does anyone know a way to determine if a Rails association has been eager loaded?  </p>
  
  <p>My situation:  I have a result set where sometimes one of the associations is eager loaded, and sometimes it isn't.  If it isn't eager-loaded, then I want to look up associations using ActiveRecord's find.  If it is eager loaded, I want to use detect.  </p>
  
  <p>For example, say that I have a "has_many" array of shipping_info objects in my item model.  Then:</p>
  
  <p>If item is eager loaded, most efficient load is: </p>
  
  <pre><code>item.shipping_infos.detect { |si| si.region == "United States" }
  </code></pre>
  
  <p>If item isn't eager loaded, most efficient load is:</p>
  
  <pre><code>item.shipping_infos.where(region: "United States").first
  </code></pre>
  
  <p>But unless I know whether it is eager loaded, I don't know which code to call to get the  record efficiently.  If I use the first method when it wasn't eager loaded, then I have to look up more DB records than necessary.  And if I use the second method when it was eager loaded, then my eager loaded objects are ignored.</p>