Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:29:19+00:00 2026-06-11T15:29:19+00:00

I am not very familiar with RoR but my colleague has written this code

  • 0

I am not very familiar with RoR but my colleague has written this code for one of our applications & I have a feeling this is just too un optimized and too many queries for a simple task.

We have 3 tables. Artist, Album & 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.

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' => 
             {  
                'song' => @toplistsongs
             }
        }
     respond_with(@w)
end

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?

Thanks for all the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:29:21+00:00Added an answer on June 11, 2026 at 3:29 pm

    You can use ActiveRecord includes method in order to reduce DB queries count. For example:

     @toplistsongs =  Song.select(INDEX_COLUMNS).order("popularity_total,created_at     DESC").limit(10).includes(:tags, { :songs => :artists }, :urls, :likers)
    

    This will produce 5 queries for the whole top method and is not dependent on the number of songs you want to present. In other words the number of queries is constant.

    You will have to use ActiveRecord associations for this to work. I can see that you are using the method get_song_artist I assume it is not made by an association.

    To form your relationship in ActiveRecord associations you will have to use the has_many, has_one and belongs_to operators.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not very familiar with jQuery but have come across this code for
I'm not very familiar with machine code, but I think this is a pretty
I am really not very familiar with javascript and its syntax, but I have
I am not very familiar with perl, but I am wondering if this would
I'm not sure why this isn't working but probably because I'm not very familiar
I'm not very familiar with routing, but here is my dilemma: I have a
I'm not very familiar in programming databases, but I have a Mysql-Database which I
I have not used jquery much yet and not very familiar with it. Trying
Not very sure if this is the right way of asking, but I was
I am not very familiar with powershell scripting and I'm stuck on this problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.