Using counter_cache with STI Models

The counter_cache option for Rails relationships is invaluable in speeding up performance by reducing the number SQL queries, but there are a couple of things that you need to do to ensure that your counts start off right and to make the whole thing work with STI-based models.