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?
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to