I am trying to display completed tasks done my user's friends.
I have a function friend?
that returns a boolean value
For Bar chart I need to use count
<%= bar_chart @completed_tasks.count%>
Here is how I try to get the desired data:
@completed_tasks = Task.where(completed: true).select{|task| friend?(task.user_id)}
And after all I need to group it by user_id. How can I do it properly?
I have tried adding the following options:
.group(:user_id)
.group_by(:user_id)
.group_by(&:user_id)
but i don't get any data represented in bar chart. What am I doing wrong?
The problem was in the fact that .group()
works with ActiveRecord Relations. Task.where(...) is an ActiveRecord Relation, while select returns an array. Here is my solution:
arr = Task.where(completed: true).select{|task| User.find(task.user_id).is_friend?(current_user)}
@completed_tasks = Task.where(id: arr.map(&:id)).group(:user_id)