Fixes #5692 - correct counts in cached_counters

This was caused by a rails bug.
Also fixes related bugs #6210, #6716 and #6813.
We should consider creating a migration to clean up any incorrect cached counters caused by these bugs.
Once we upgrade to rails 4.x we can remove this mixin.