Rails 3 aggregate functions with timestamps
I want to sum up the differences between two time columns in a table. I'm using the following code but it returns zero because end_time is not getting parsed correctly. Both columns are timestamps.
table.sum("end_time - start_time")
I found that when I typed in table.sum("end_开发者_如何学JAVAtime"), the sum was 2010. This is odd because there is one row in table with an end_time of "2010-12-18 23:42:30".
You can't "subtract" times unless they are UNIX timestamps...
Just let rails do the work. Write a query like:
t = Table.find(:all, :conditions => {...})
t.each do |a|
puts a.end_time - a.start_time
end
精彩评论