开发者

Rails 3 aggregate functions with timestamps

开发者 https://www.devze.com 2023-01-31 02:10 出处:网络
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.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消