ActiveRecord query, order by association, last of has_many
<p>Im trying to list all <em>Users</em> by the <code>created_at</code> column of the most recently created associated recored (communications).  </p>

<p>What I have so far:  </p>

<pre class="lang-rb prettyprint-override"><code>User.includes(:communications).order(
  'communications.created_at IS NULL, communications.created_at asc'
)
</code></pre>

<p>As it is, <code>desc</code> works as I expect. The issue is when the order is reversed and I try order <code>asc</code>. It appears that is's because the <em>user</em> can have many <em>communications</em>, The query returns the list of <em>users</em> in order of the first communications created instead of the most recent.  </p>

<p>How can I modify the query to target the most recently created associated records in both <code>asc</code> and <code>desc</code> order?</p>

<p>Thanks for you time.</p>