I have two models: Company and Transaction (has-many relationship). Transaction model has balance
attribute. I have a query to join the models:
scope :joined_transactions, (lambda do
select('transactions.balance as current_balance')
.joins('LEFT OUTER JOIN transactions ON transactions.company_id = companies.id')
end)
However, I want to include only the last transaction into this query. As a result Company.joined_transactions.first.current_balance == Company.first.transactions.last.balance
should be true.