Muhammad Faisal Iqbal
Muhammad Faisal Iqbal

Reputation: 1836

Rails most occurrence of day of week from date field

In rails 5 project with postgres I have to get most occurrence of day of week from date time field Like Log table

ID date
1  01-12-2017  //Day is Friday here
2  01-12-2017  //Day is Friday here
3  02-12-2017  //Day is Saturday here
4  02-11-2017  //Day is Thursday here
5  02-10-2017  //Day is Monday here

So the most occurred day of week is FRIDAY with count 2 against ID 1 & 2

I can group by with week of day to get such results?

Upvotes: 0

Views: 125

Answers (1)

inye
inye

Reputation: 1796

you can use the gem groupdate

And you can do a query like:

records = YOURMODEL.group_by_day_of_week(:date).count
day = Hash[records.sort_by{|k, v| v}.reverse].first[0]

this day will have day like 1,2 etc to show monday, tuesday.

Upvotes: 1

Related Questions