title: How to group an Active Record relation on a has_many :through association
body: <p>What I want to do is group records in an Active Record relation by a column on the :through model of a has_many :through relation, but I don't know what the best way is to go about it.</p>
  
  <p>First, my models:</p>
  
  <pre><code>User has_many :subscriptions
       has_many :courses, :through =&gt; :subscriptions
  
  Subscription belongs_to "both"
  
  Course has_many :subs...
         has_many :users, :through =&gt; :subscriptions
  </code></pre>
  
  <p>I grab the courses related to the current_user like so: <code>@courses = current_user.courses</code>
  And I want to be able to do something like: <code>@courses = current_user.courses.group('subscriptions.state')</code> &lt;-- That doesn't work obviously.</p>
  
  <p>Also, after the records are grouped, what's the proper way to loop through that in the view to display them? In another place in my app, I've used group_by and each_pair respectively to accomplish this, but that was with one individual model.</p>