title: Rails postgres order by custom
body: <p>I am searching for records using an IN query likes so:</p>
  
  <pre><code>Event.where("events.id IN (#{events.nil? ? '' : events.collect(&amp;:id).join(',')})")
  </code></pre>
  
  <p>Which produces output like:</p>
  
  <pre><code>SELECT "events".* FROM "events" WHERE (events.id IN (143,169,139,48,172,146,145,124,111,49,108,18,113,144)) AND (status = 'live') LIMIT 10
  </code></pre>
  
  <p>However, this is then sorted by update_at by default. I'd like it to sort by the IN part of the query. What I mean is that I'd like this to be the order it returns in:</p>
  
  <pre><code>143,169,139,48,172,146,145,124,111,49,108,18,113,144
  </code></pre>
  
  <p>Is that possible at all?</p>
  
  <p>I must also note that I can't add a priority column or similar to the table as the "events" part will be dynamically generated.</p>
  
  <p>I've just discovered a very similar question here:</p>
  
  <p><a href="https://stackoverflow.com/questions/12012574/postgres-order-by-values-in-in-list-using-rails-active-record">Postgres ORDER BY values in IN list using Rails Active Record</a></p>
  
  <p>Feel free to mark this one of mine as duplicate</p>