title: How do you use the postgresql WITH in activerecord?
body: <p>In postgresql one can read data from a "with" I want to know how to make use of that in rails without putting the whole query in raw sql.. </p>
  
  <p>Here's a sample query: it is totally contrived for this question.  </p>
  
  <pre><code>with tasks as (select 1 as score, tasks.* from tasks)
  select 1 from tasks where id &gt; 10 order by tasks.score, tasks.id
  </code></pre>
  
  <p>In my real example score is calculated not just 1 but for the example it works. </p>
  
  <p>Here's how I imagine the code would look</p>
  
  <pre><code>Task.with('tasks as (select 1 as score, tasks.* from tasks)')
      .where('id &gt; 10')
      .order(score)
      .order(id)
  </code></pre>
  
  <p>I don't really like using the "with" because it is PG specific, but I really need to sort on the calculated value. I tried a view but the creation of the view in PG requires the exact fields, and I don't want other coders to have to alter the view when they alter the source table. </p>
  
  <p>I do really want to be able to chain this. </p>