1

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.

1
  • You need to count a specific date or how many times each date repeated? Commented Feb 19, 2020 at 18:54

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

undefined method `transform_values' for #<Hash:0x0000561021013038>
codevb, Hash#transform_values was introduced in Ruby v2.4. Does that explain your finding?
i want, count same date time was returning from postgresql using each.
Hmm, I've been doing this for years and have never seen the syntax where you chain a method off the end statement. I guess I've done similar things using the single line method { |x| something }.something_else. I guess they're the same under the hood?
@Dan Yeah it is not the prettiest thing, but I prefer to be consistent and always use do ... end for multiline blocks anyway
1

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]

5 Comments

That means you would need to enumerate the array for each of its elements, which is extremely inefficient.
But how you count suitable elements without enumerating them all? Only if we group tickets by date on DB level but it is a different approach.
tested, it worked fine is showing the count of equal dates after reading 20 records thankyou !!
Compare what you have with @max's solution (especially the use of Enumerable#tally). Both of the calculations he gives require a single pass through the array to obtain the counts for all unique elements in the array. You are making a pass through the array for each unique element in the array.
But this is the point. The question is how to count how many times the certain date is present. count method is made for this purpose, use group_by is overcomplication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.