Forum: Ruby on Rails How to add something similar to :counter_cache - :avg_cache

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 Iván Vega Rivera (Guest)
on 2006-03-08 03:21
(Received via mailing list)
Hi,

I was thinking of doing this with a stored procedure, but I thought it
could be better implemented in Rails.

I have several tables that hold a counter cache for other tables,
which aren't directly related. For example:

Table 'schools' has records that belong_to table 'counties', and that
table has records that belong_to table 'states'.

In both states, and counties, there's a column named 'schools_total'.
I don't use Rails built in counter cache because schools don't belong
directly to states.

So I want to implement two features. Inside counties:

counter_cache :schools
avg_cache :schools

Inside states:

counter_cache :schools, :through => counties
avg_cache :schools, :through => counties

The first case is straightforward, but on the second case, the
"caches" would need to get updated whenever the counties table is
modified (apart from when the schools table is modified), because
there's no direct relation between the schools and the states table.

And then I want that each county & state object have a method named
schools_count and schools_avg, which would retrieve their respective
column from the db.

This is *somewhat* over my head, but what the hell, I like challenges.
So could someone help me out?

Kind regards,
Ivan V.
This topic is locked and can not be replied to.