Adding Time converting to seconds in Ruby

3

I'm trying to find the average difference between the creation date and the last update of the records in a table as follows:

tickets = Ticket.all.where('updated_at IS NOT NULL')
t = tickets.sum(:updated_at, conditions: 'updated_at.to_time') - tickets.sum(:created_at, conditions: 'created_at.to_time')

But this is giving error, where am I wrong?

I did it this way below and it worked; but I believe that without doing this loop I would gain in performance.

tickets = current_client.tickets.where('updated_at IS NOT NULL')
t = 0
tickets.each do |ticket|
  t += ticket.updated_at.to_time - ticket.created_at.to_time
end
    
asked by anonymous 11.03.2015 / 15:24

1 answer

2

If the problem is performance, it would do the operation directly in the DB:

Ticket.average("updated_at - created_at")

If there are other conditions, something like:

Ticket.where(<suas condições>).average("updated_at - created_at")
    
16.03.2015 / 20:22