Cache Magic field for totaling


#1

Hello,
Rails provide counter cache field for associations, I want a similar
cache
for totaling up values
(say account_balance = sum_of_deposits - sum_of_withdrawals).

I was wondering if rails, already has another surprise in store for me,
or is
there any plug-in or how-to for doing these kind of things.

Thanks.


Surendra S.
http://ssinghi.kreeti.com, http://www.kreeti.com
Read the latest news at: http://news.kreeti.com
,----
| “O thou my friend! The prosperity of Crime is like unto the lightning,
| whose traitorous brilliancies embellish the atmosphere but for an
| instant, in order to hurl into death’s very depths the luckless one
| they have dazzled.” – Marquis de Sade
`----


#2

Surendra S. removed_email_address@domain.invalid writes:

Rails provide counter cache field for associations, I want a similar cache
for totaling up values
(say account_balance = sum_of_deposits - sum_of_withdrawals).

OK. I went ahead and wrote something of my own. For this purpose I had
to
extend the rails internal a bit, it is working but I was wondering
whether I
have done things the right way.

[1] Is there any way of adding one more option to the ‘belongs_to’
declaration? I didn’t find any so I wrote my own declaration titled
‘belongs_to_opt’. But there is a little bit of redundancy in the my
implementation of belongs_to_opt' and rails internalbelongs_to’, I
was
wondering if there was a way of getting rid of that.

[2]In the rails wiki to extend ActiveRecords `append_features’ is used
http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample

I was wondering shouldn’t it be included', because in the Pragmatic book it is mentioned thatincluded’ is the preferred way.

I have extended ActiveRecords in the following way, does it follows the
convention, does the style of extending looks OK?

module ActiveRecord
module Associations # :nodoc:
def self.append_features(base)
super
base.extend(ClassMethods)
end

module ClassMethods
  def belongs_to_opt(association_id, options = { })
           .......
end

class AssociationCollection
   ....
end

end
end

It is my first foray into peeking at the rails core, and I am really
impressed
by the clarity of the code. It was so easy to understand and grasp the
flow. My first impression is that Rails not only helps in writing
beautiful
code, but itself is written beautifully.

Any tips will be appreciated.
Thanks.


Surendra S.
http://ssinghi.kreeti.com, http://www.kreeti.com
Read the latest news at: http://news.kreeti.com
,----
| Great wits are sure to madness near allied,
| And thin partitions do their bounds divide.
|
| (John Dryden, Absalom and Achitophel, 1681)
`----