title: Finding all rows with unique combination of two columns
body: <p>I have this table <code>messages</code>;</p>
  
  <pre><code>sender_id    recipient_id
  1            2
  1            3
  1            3
  2            1
  3            1
  2            3
  </code></pre>
  
  <p>I wish to select rows such that:</p>
  
  <ol>
  <li>Either <code>sender_id</code> or <code>receiver_id</code> = <code>current_user.id</code>.</li>
  <li>The other field should be unique.</li>
  </ol>
  
  <p>I.e. I want to select unique from table where <code>sender_id = 2</code> or <code>recipient_id = 2</code> and I need this result:</p>
  
  <pre><code>sender_id    recipient_id
  2            1
  2            3
  </code></pre>
  
  <p>How to do it?<br>
  Why? Because I wish to build a facebook-like inbox in which sent and received messages are aggregated, and this query is the bottleneck so far.</p>
  
  <p>I am using rails 3.2 and Postgres 9.3.</p>