title: How to fetch an associated model's last record to prevent N+1 queries?
body: <p>I have a model <code>Invoice</code> which <code>has_many</code> <code>Payments</code> and a model <code>Payment</code> that <code>belongs_to</code> <code>Invoice</code>.</p>
  <p>We export Invoice data monthly in batches, and we need each Invoice's last Payment.</p>
  <p>In our view we are currently doing <code>Invoice.payments.last</code> once for each Invoice that we are exporting, and I was asked to prevent N+1 queries.</p>
  <p>I don't understand if I should add this query in the controller or in the Invoice model, or if it should be a <code>has_one :last_payment</code> association or a scope.</p>
  <p>Any help would be appreciated.</p>