Question: How can I make a grouped array like this?
event_id: 1, person_id: 1, tasks: [1, 2]
event_id: 1, person_id: 2, tasks: [3]
event_id: 2, person_id: 1, tasks: [4]
The givens:
Tasks table:
Event.rb:
has_many :tasks
has_many :people
Task.rb
belongs_to :event
Person.rb
belongs_to :event
My thoughts are to do something like this (doesn't work):
- @grouped_tasks = Task.all.group(:event_id, :person_id).each do |z|
= z.event.id
= z.member.id
= z.tasks.collect{|r| r.task.name}.join(",")
= z.tasks.pluck(:name)

has_and_belongs_to_manyor anhas_many :throughassociation. You find out which to pick here.event_id: 1, person_id: 1, tasks: [1, 2]arr = [{ event_id: 1, person_id: 1, tasks: [1, 2]}, {...}, {...}]. Notice that I assigned a variable (arr) to that array. That way readers can refer to that variable in answers and comments without having to define it. Please also assign a variable to the task table object. In general, do not use pictures of code or data.