Reputation: 95
I execute the query in the bank by the controller and I receive the data in the var when executing each on top of the var returns me an array, I wanted to count how many times the same parameter is repeated to create a counter could help me follow my code.
Rails 3.2, ruby 2.3
results os my query.. from postgresql
#<tickets title: "any text any first register ", updated_at: "2018-01-30 18:38:58", company: 89869>
#<tickets title: "any text any second register ", updated_at: "2018-06-25 20:18:20", company: 99991>
I want to count how many times the same date is repeated regardless of the time to count the number of times that displays the same date.
Upvotes: 1
Views: 822
Reputation: 3191
For a specific value you can use count
method:
date = Date.today # Put your date here
tickets.count { |ticket| ticket.updated_at.to_date == date }
UPD
Group tickets by date and take the result from the hash. I am not sure if it works in Rails 3.2
date = Date.today # Put your date here
Ticket.group('date(updated_at)').size[date]
Upvotes: 1
Reputation: 26758
My usual approach is to use group_by
and transform_values
.
counts_by_date = tickets.group_by do |ticket|
ticket.updated.at.strftime("%Y/%m/%d") # get Year/Month/Day as string
end.transform_values(&:count)
With Ruby 2.7 we get tally
which is slightly shorter:
counts_by_date = tickets.map do |ticket|
ticket.updated.at.strftime("%Y/%m/%d") # get Year/Month/Day as string
end.tally
Upvotes: 2