3
I am trying to find the average difference between the creation date and the last update of the records of 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 way you’re making a mistake, where I’m making a mistake?
I did so below and it worked; but I believe without doing this loop I would win 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
What is the expected result? What is the result obtained?
– mutlei
The expected would be the average difference in seconds of the updated_at - created_at but it returns nothing, gives a query error.
– Daniel
I was thinking of an example with real data. If
a = b - c
, withb
being 3 andc
being 2, the expected would be1
buta
received-1
.– mutlei