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, :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?