Problem with mysql & Rails for stats

Hi everybody

I have a ROR application which generates some stats taking data from a
db table.
To get the results I before create an array with all the values involved
in the stats - each data stack label - ... then calculate the time
interval splited for week or month... and for each splitted interval I
have an Activerecord calculte(:count) method in order to create - for
each data stack - an array of values...

This seems to work done, but sometimes something goes wrong, couse I
return empty or nil array of results. I fixed it with a control to avoid
system errors. But Anyway it happens... And I loose some data in my
stats.

Looking at the mysql query log, all the query are executed...
How can I menage this situation? Is it possible to observ the
ActiveRecord::calculate feedback to understand exactly what
randomly happens with these query?

here is an abstract of the code