title: Rails array group_by
body: <p>I have a dynamic array of object result from a database query.</p>
  
  <pre><code>USERNAME   choice_indx   legend
  USER1        3             4
  USER2        0             4
  USER3        0             4
  USER1        9             2
  USER2        9             2
  USER3        8             2
  USER1        3             1
  USER2        9             1
  USER3        8             1
  </code></pre>
  
  <p>Query:</p>
  
  <pre><code>SELECT survey_answers.anonymous_user_id, survey_answers.choice_index, survey_questions.legend 
    FROM `survey_answers` 
    LEFT JOIN surveys ON surveys.id = survey_answers.survey_id 
    LEFT JOIN survey_questions ON survey_questions.id = survey_answers.survey_question_id 
    WHERE (survey_questions.legend IN (1,2,4)) AND (survey_answers.track_id =2) AND (survey_answers.survey_id =2) AND (surveys.survey_type =2)
  </code></pre>
  
  <p>How can I group this by user and result it like this:</p>
  
  <pre><code>final_array = {
    "USER1" =&gt; [[3,4],[9,2],[3,1]], 
    "USER2" =&gt; [[0,4],[9,2],[9,1]],
    "USER3" =&gt; [[0,4],[8,2],[8,1]]
  }
  </code></pre>
  
  <p>I've tried using group_by in rails, but the result was not the same from what I want. Can anyone lend me a hand? Thanks.</p>