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

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

arr_data_stack.each do |x|
values_array = []
CalculateInterval.each do |interval|
values_array << Log.calculate(:count, [… ]) if x
results[x] = (values_array .empty? ? 0 : values_array)

the problem is the result[x] is sometimes empty or nil - I don’t know
exactly - even if all the query were executed on db

let me know your opinion please

thanks a lot