How to join tables to get only the last record of has-many association?
<p>I have two models: Company and Transaction (has-many relationship). Transaction model has <code>balance</code> attribute. I have a query to join the models:
</p>

<pre><code>scope :joined_transactions, (lambda do
  select('transactions.balance as current_balance')
  .joins('LEFT OUTER JOIN transactions ON transactions.company_id = companies.id')
end)
</code></pre>

<p>However, I want to include only the last transaction into this query. As a result <code>Company.joined_transactions.first.current_balance == Company.first.transactions.last.balance</code> should be true.</p>