Answered counts can be negatively offset
Apparently some user managed to offset their answered count by 1 into negative. It's questionable how this happened, and probably should be fixed.

This is a fun one -- looks like that user somehow managed to delete a single answer twice at the same time. The original question ended up twice in their inbox, too.
I see a few ways how to solve this:
- This special case might be solvable by just creating a unique index on
inboxes (user_id, question_id), however this might still occur with comments, smiles, and questions. - We change how the answers and whatnot are counted. Right now we have these
*_countcolumns in the user model; we could get potentially get rid of those and calculate them on the fly -- perhaps also cache them somewhere (and, of course, invalidate them once something created or destroyed). For a user with 12.6k smiles calculating the smiled_count takes44.470msinitially, and14.059msafterwards. - We keep the current logic in place, but have a cron job running that ensures those counters are up-to-date. That might run once a day or so.
We change how the answers and whatnot are counted. Right now we have these *_count columns in the user model; we could get potentially get rid of those and calculate them on the fly -- perhaps also cache them somewhere (and, of course, invalidate them once something created or destroyed). For a user with 12.6k smiles calculating the smiled_count takes 44.470ms initially, and 14.059ms afterwards.
I opt for this solution, I think we used Rails.cache for this in another project and it worked well. We can just invalidate the cache on answering a question or creating one, and otherwise leave it fixed.
This also would drop more user fields, which we strive for, to keep the model as lean as possible.