title: List and Group Orders by Artist and Songs
body: <p>I have created a functioning e-commerce platform where Members can buy songs. Everything works fine, But I would like to group all of my Orders in my Index Page by Artist.</p>
  
  <p>Currently I am able to group each Author with its corresponding albums, But each ordered song is then listed multiple times under each album. </p>
  
  <p><strong>How can I list all of the Ordered Songs corresponding to an Album without duplicating the Song Title and Providing a Counter that shows the Total Orders?</strong></p>
  
  <pre><code>EX. of what I'd like in my Orders Page
  
  ###Orders grouped by Artists (which I've Done)
  Artist1
    Album1                      ###List of Albums Corresponding to an Artist 
      --Song1 (10 0rders)          (which I've Done)
      --Song3 (5 Orders)
    Album2 
      --Song5 (2 Orders)        ###Ordered Songs Corresponding to an Album (NEED HELP)
    Album3                      ###No Duplicates, only list ordered songs with a counter 
  
  Artist2
    Album1 
      --Song2 (1 Orders)
    Album2 
  
  Artist3
    Album1 
      --Song4 (1 Orders)
  </code></pre>
  
  <p>MODELS</p>
  
  <pre><code>class Order &lt; ActiveRecord::Base
    attr_accessible :artist_id, :album_id, :user_id, :order_date
  
    belongs_to :song
    belongs_to :user
  
  end
  
  class Artist &lt; ActiveRecord::Base
    attr_accessible :name
  
    has_many :albums
    has_many :songs, :through =&gt; :albums
    has_many :orders, :through =&gt; :songs
  
  end
  
  class Album &lt; ActiveRecord::Base
    attr_accessible :name, :artist_id
  
    belongs_to :artist
    has_many :songs
    has_many :orders, :through =&gt; :songs
  
  end
  
  class Song &lt; ActiveRecord::Base
    attr_accessible :artist_id, :album_id, :title, :price
  
    belongs_to :album
    has_many :orders
  
  end
  </code></pre>
  
  <p>CONTROLLERS</p>
  
  <pre><code>def index    
    @artists = Artist.includes(:orders).where('orders.id IS NOT NULL')    
  end
  </code></pre>
  
  <p>VIEWS</p>
  
  <pre><code>&lt;% @artists.each do |artist| %&gt;   ###Lists All Artista with Orders
    &lt;h3&gt;&lt;%= artist.name %&gt;&lt;/h3&gt;
  
    &lt;% artist.albums.each do |album| %&gt;   ###Lists all Albums Corresponding to Artist
        &lt;h4&gt;&lt;%= album.name %&gt;&lt;/h4&gt;
  
       &lt;% album.orders.each do |order| %&gt; ###Lists All Ordered Song Titles &amp; Price
         &lt;%= order.song.title %&gt;&lt;br&gt;      ###How can i list each ordered song with a counter
         &lt;%= order.song.price %&gt;&lt;br&gt;
       &lt;% end %&gt;
  
    &lt;% end %&gt;
  
  &lt;% end %&gt;
  </code></pre>