I have this table messages;
sender_id    recipient_id
1            2
1            3
1            3
2            1
3            1
2            3
I wish to select rows such that:
- Either sender_idorreceiver_id=current_user.id.
- The other field should be unique.
I.e. I want to select unique from table where sender_id = 2 or recipient_id = 2 and I need this result:
sender_id    recipient_id
2            1
2            3
How to do it?
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.
I am using rails 3.2 and Postgres 9.3.
 
     
    