# Cache Magic field for totaling

**URL:** <https://www.ruby-forum.com/t/cache-magic-field-for-totaling/54273>\
**Category:** Rails\
**Created:** [February 28, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/cache-magic-field-for-totaling/54273 "2006-02-28T13:38:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [February 28, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/cache-magic-field-for-totaling/54273/1 "2006-02-28T13:38:06Z")

</div>

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://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read the latest news at: [http://news.kreeti.com](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  
`----

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [March 3, 2006, 3:08pm UTC](https://www.ruby-forum.com/t/cache-magic-field-for-totaling/54273/2 "2006-03-03T15:08:37Z")

</div>

Surendra S. [removed\_email\_address@domain.invalid](mailto: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 internal `belongs\_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](http://wiki.rubyonrails.com/rails/pages/ExtendingActiveRecordExample)

I was wondering shouldn’t it be `included', because in the Pragmatic book it is mentioned that `included’ 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://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read the latest news at: [http://news.kreeti.com](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)  
`----
