title: Extract Mongoid documents based on the DateTime of their last has_many relations?
body: <p>I have a bunch of <code>orders</code>, and some of them have <code>order_confirmations</code>.</p>
  
  <p>1: I wish to extract a list of <code>orders</code> based on the <code>DateTime</code> of its last <code>order_confirmation</code>. This is my failed attempt (returns 0 records):</p>
  
  <pre><code>Order.where(:order_confirmations.exists =&gt; true).desc("order_confirmations.last.datetime")
  </code></pre>
  
  <p>2: I wish to extract a list of <code>orders</code> where the last <code>order_confirmation</code> is between 5 and 10 days old. This is my failed attempt (returns 0 results):</p>
  
  <pre><code>Order.lte("order_confirmations.last.datetime" =&gt; 5.days.ago).gte("order_confirmations.last.datetime" =&gt; 10.days.ago)
  </code></pre>
  
  <p>My relations:</p>
  
  <pre><code>class Order
    include Mongoid::Document
    has_many :order_confirmations
  end
  
  class OrderConfirmation
    include Mongoid::Document
    field :datetime, type: DateTime
    belongs_to :order
  end
  </code></pre>