I am using this function in my User model to populate a select box in a form:
def outstanding_invoices
invoices.to_a.select { |invoice| invoice.balance < 0 }
end
The problem is that balance is not a database field but a method that calculates something.
So it takes an awful lot of SQL queries to generate the select box because the function above iterates over every single invoice that the user has.
Is there a faster way to generate the select box, ideally using one SQL query only?
A simple alternative would probably be storing the balance of each invoice in the database, but I am somehow reluctant to do that because I want to keep my database to a minimum.
Thanks for any help.