title: Order with DISTINCT ids in rails with postgres
body: <p>I have the following code to join two tables microposts and activities with micropost_id column and then order based on created_at of activities table with distinct micropost id. </p>
  
  <pre><code> Micropost.joins("INNER JOIN activities ON 
   (activities.micropost_id = microposts.id)").
   where('activities.user_id= ?',id).order('activities.created_at DESC').
   select("DISTINCT (microposts.id), *")
  </code></pre>
  
  <p>which should return whole micropost columns.This is not working in my developement enviornment.</p>
  
  <pre><code>(PG::InvalidColumnReference: ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
  </code></pre>
  
  <p>If I add activities.created_at in SELECT DISTINCT, I will get repeated micropost ids because the have distinct activities.created_at column. I have done a lot of search to reach here. But the problem always persist because of this postgres condition to avoid random selection.</p>
  
  <p>I want to select based on order of activities.created_at with distinct micropost _id. </p>
  
  <p>Please help.. </p>