title: Ruby on Rails: Is this the right way to structure code in Ruby on Rails? This is too slow
body: <p>I am not very familiar with RoR but my colleague has written this code for one of our applications &amp; I have a feeling this is just too un optimized and too many queries for a simple  task. </p>
  
  <p>We have 3 tables. Artist, Album &amp; Song. Artist can have multiple albums. Album can have multiple songs. We are trying to output the top 10 songs based on a field popularity_total in the songs table. Then there are other tables that capture likes etc.</p>
  
  <pre><code>def top
       # a list of 10 most played songs in the past week
       @toplistsongs =  Song.select(INDEX_COLUMNS).order("popularity_total,created_at     DESC").limit(10)
       @toplistsongs.each do |song|
        song['urls'] = song.get_song_urls
        song['artist'] = song.get_song_artist
        song['genre'] = song.tag_list
        song['hearts'] = song.likers(User).count
        song['like_status'] = ( current_user!=nil ? current_user.likes?(song) : false )
        song['impressions'] = song.impressionist_count
        song['albums'] = song.get_song_album
      end
  
      @w = {'top' =&gt; 
               {  
                  'song' =&gt; @toplistsongs
               }
          }
       respond_with(@w)
  end
  </code></pre>
  
  <p>Every fetch inside the loop with result in a hit on the db. I have a feeling there are too many queries happening in the loop for each song when ideally they can all be done using single query for all the songs. Can anyone suggest if this is a standard rails way of handling things or is this outright bad code?</p>
  
  <p>Thanks for all the help.</p>