title: Rails: How do I place ActiveRecord query results into an array using a loop?
body: <p><strong>Context:</strong></p>
  
  <p>I have a <code>Company</code> model that has many <code>projects</code>. Each <code>project</code> has many <code>tasks</code>. The <code>company</code> has many <code>employees</code>. </p>
  
  <p>The <code>Employee</code> model is associated with <code>Company</code> and not with <code>projects</code> or <code>tasks</code>. The <code>Task</code> model does have a <code>employee_name</code> attribute, since it is one employee per task (but more task per employee). An employee will have only one <code>task</code> per <code>project</code>.</p>
  
  <p><strong>Schema:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/2s0Xk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2s0Xk.png" alt=""></a></p>
  
  <p><strong>My question:</strong></p>
  
  <p>I'm trying to create an array that for each <code>employee</code> stores the <code>projects</code> it has a <code>task</code> in. </p>
  
  <p>I currently have the following code working, but I am wondering if I could do this in a better way. Should I change my model associations, or is this the way to go?</p>
  
  <pre><code>        @employee = Employee.find(1)
          @employee_tasks = Task.where(employee: @employee.name)
  
          @employee_project = {}
          i = 0
  
          @employee_tasks .each do |f|
              i += 1
              @employee_project[i] = Project.where(id: f.project_id)              
          end
  </code></pre>