title: How to use "DISTINCT ON" in conjunction with Arel / ActiveRecord?
body: <p>I have a <code>Message</code> ActiveRecord Model and I want to select all messages that have a distinct <code>conversation_partner_id</code>. I'm currently specifying the <code>DISTINCT ON</code> clause as a string but I was wondering if it could be provided as an <code>Arel</code> expression instead and whether there would be a benefit. My statement looks as follows:</p>
  
  <pre><code>Message.select('DISTINCT ON ("messages"."conversation_partner_id") messages.*').from(t)
  </code></pre>
  
  <p>where <code>t</code> is a union of the <code>messages</code> table and specified as a somewhat complex <code>Arel</code> expression. I leave it out as it is independent of the <code>SELECT</code> issue and works just fine.</p>
  
  <p>The following code does <strong>not</strong> work:</p>
  
  <pre><code>Message.select([
    Arel::Nodes::DistinctOn.new(Message.arel_table[:conversation_partner_id]),
    Message.arel_table[Arel.star]
  ]).from(t)
  </code></pre>
  
  <p>It results in malformed SQL as it places a comma after the <code>DISTINCT ON ()</code> clause:</p>
  
  <pre><code>SELECT DISTINCT ON ("messages"."conversation_partner_id"), "messages".* ...
                                                           ^
  </code></pre>