I am considering how to approach the problem of providing client
balances. I have considered treating balances as virtual attributes in
some fashion approximating this:
class Client < AR
…
has_many :ar_transactions, :class => ‘Transaction’,
:conditions => { :type => ‘ar’,
:valid? = true }
def balance
self.ar_transactions.sum(:balance)
end
I am also thinking, at the moment, that the balance of a transaction is
similarly a virtual attribute that might look like this:
class Transaction < AR
…
has_many :details, :class => ‘TransactionDetail’,
:conditions => { :valid? = true, …
has_many :offsets, :class => ‘TransactionJournal’,
:conditions => { …
I do not account for the separate treatment of debit and credit items
in this example
def balance
offset = self.offsets.sum(:amount)
total - offset
end
def total
self.details.sum(:amount)
end
Is this the way I should do this? Is there a better way?