Group by week in Rails 4/Mysql db

Viewed 1242

Is there any way to group by week in rails AR

answers = Answer.all

daily_answer_count = {}
answer.where("val = 5").group('date(created_at)').count.map{|k,v| daily_answer_count["#{k.strftime("%Y-%m-%d")}"] = v}

Output
{ 
"2016-03-05":  65
"2016-03-06":  57
"2016-03-07":  45
}

So the problem know is i need to create a hash map like above for weekly biases so the key would be beginning of the week and value will be the total value of the week.

Any direct way of doing this using rails AR

3 Answers
Related