title: Active Record - How to perform a nested select on a second table?
body: <p>I need to list all customers along with their latest order date (plus pagination).</p>
  
  <p>How can I write the following SQL query using Active Record?</p>
  
  <pre><code>select *,
    (
      select max(created_at)
      from orders
      where orders.customer_id = customers.id
    ) as latest_order_date
  from customers
  limit 25 offset 0
  </code></pre>
  
  <p>I tried this but it complains <code>missing FROM-clause entry for table "customers"</code>:</p>
  
  <pre><code>Customer
    .select('*')
    .select(
      Order
        .where('customer_id = customers.id')
        .maximum(:created_at)
    ).page(params[:page])
  
  # Generates this (clearly only the Order query):
  SELECT MAX("orders"."created_at") 
  FROM "orders" 
  WHERE (customer_id = customers.id)
  </code></pre>
  
  <p><strong>EDIT</strong>: it would be good to keep AR's parameterization and <a href="https://github.com/kaminari/kaminari" rel="nofollow noreferrer">kaminari</a>'s pagination goodness.</p>