title: ActiveRecord format joins into an array
body: <p>I would like to improve the efficiency of one of my endpoints. Here's the rundown:</p>
  
  <p>In a mysql database, we have three example tables, two of which are join tables to the first.</p>
  
  <pre class="lang-rb prettyprint-override"><code>:user has_many: :orders 
  :user has_many: :attributes
  </code></pre>
  
  <p>The sequel query is simple (but subject to change):</p>
  
  <pre class="lang-rb prettyprint-override"><code>User.left_joins(:orders, :attributes).select("users.*, orders.*, attributes.*")
  </code></pre>
  
  <p>This query returns big objects, for every user, an order or attribute where it exists, it returns a flat object.</p>
  
  <pre class="lang-rb prettyprint-override"><code>[
    {
      User:
       id: 1,
       order_id: 1,
       order_columns_here,
       attribute_id: 1,
       attribute_columns_here
    },
    {
      User:
       id: 1,
       order_id: 2,
       order_columns_here,
       attribute_id: 2,
       attribute_columns_here
    }
  ]
  </code></pre>
  
  <p>The result is an array of the same user data repeated multiple times for every order or attribute associated to it.</p>
  
  <p>If I have to write some ruby to format this object the way the API is requested, its going to be slow (we have a huge db). Is there an active record method that I can use to augment this query so the data is returned with all the photos and floor_plans in an array?</p>
  
  <p>i.e.</p>
  
  <pre class="lang-rb prettyprint-override"><code>[
    {
      User: id: 1, orders: [all_order_data_for_user_1], attributes: [all_attributes_data_for_user_1]
    }
  ]
  </code></pre>
  
  <p>The last couple years I've been using the Sequel gem, which does this for me. I've read through much of the AR docs, and am not sure what would help me here from the ActiveRecord world.</p>
  
  <p>Any ideas?</p>
  
  <p><em>EDIT</em></p>
  
  <p>Mysql version: mysql  Ver 14.14 Distrib 5.7.29
  Rails: 5
  Ruby: 2.3.1</p>