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 8105667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:13:21+00:00 2026-06-06T00:13:21+00:00

User has many Tracks, through Favorite. Favorite has some extra per-user meta-data about the

  • 0

User has many Tracks, through Favorite. Favorite has some extra per-user meta-data about the related track, and the whole thing is returned as a json blob using custom :as_public hashing method.

Even though I’m accessing the related objects using a JOIN, I’m making hundreds of very basic SELECT track FROM tracks WHERE track.id='1' queries. I want to optimize this lookup.

users_controller:

def show
  @user = User.find(params[:id])
  
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @user.to_json(:methods => [:favorites_as_public_tracks]) }
  end
end

user.rb

def favorites_as_public_tracks
  favorites.joins(:track).sort_by(&:created_at).map(&:as_public_track)
end

favorite.rb

class Favorite < ActiveRecord::Base
  belongs_to :user
  belongs_to :track
  
  #Grabs some stuff from Favorite, merging it with the public data from Track
  def as_public_track
    track.public_attributes.merge(public_attributes_for_merging_onto_track)
  end
  
  # This stuff gets added onto track.to_json and used by javascript
  def public_attributes_for_merging_onto_track
    return {
      :favorite_id  => id,
      :from_service => from_service,
      :favorited_at => created_at,
      :collection_name => "#{collection_name}, #{from_service}"
    }
  end
    
  def public_attributes
    private_attrs = [:user_id]
    
    attributes.reject {|key, val| private_attrs.include? key.to_sym }
  end
end

track.rb

  def public_attributes
    private_attrs = [] #[:id]
    
    attributes.reject {|key, val| private_attrs.include? key.to_sym }
  end

The SQL that gets run when I access the user’s favorites as public tracks is:

User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", "1"]]
  Favorite Load (7.8ms)  SELECT "favorites".* FROM "favorites" INNER JOIN "tracks" ON "tracks"."id" = "favorites"."track_id" WHERE "favorites"."user_id" = 1 ORDER BY "favorites".created_at DESC
  Track Load (1.3ms)  SELECT "tracks".* FROM "tracks" WHERE "tracks"."id" = 1 LIMIT 1
  Track Load (0.5ms)  SELECT "tracks".* FROM "tracks" WHERE "tracks"."id" = 2 LIMIT 1
  Track Load (0.3ms)  SELECT "tracks".* FROM "tracks" WHERE "tracks"."id" = 3 LIMIT 1
  Track Load (0.3ms)  SELECT "tracks".* FROM "tracks" WHERE "tracks"."id" = 4 LIMIT 1
  Track Load (0.5ms)  SELECT "tracks".* FROM "tracks" WHERE "tracks"."id" = 5 LIMIT 1
  Track Load (0.2ms)  SELECT "tracks".* FROM "tracks" WHERE "tracks"."id" = 6 LIMIT 1
  Track Load (0.2ms)  SELECT "tracks".* FROM "tracks" WHERE "tracks"."id" = 7 LIMIT 1
  Track Load (0.2ms)  SELECT "tracks".* FROM "tracks" WHERE "tracks"."id" = 8 LIMIT 1

How do I do this without making hundreds of SELECT track where track.id=’…’ queries?

Thanks!

  • 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-06T00:13:22+00:00Added an answer on June 6, 2026 at 12:13 am

    If you have a list that stays the same for all instances, consider making it a class method rather than an instance method. See if you can make the public attributes and private attributes lists as class methods. Otherwise, to build these lists for each instance, you will get a hit for each record.

    Usually, :include :tracks in the finds can fix it, but I don’t think that is the issue here.

    Good luck!

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

Sidebar

Related Questions

I wrote some code to automatically track many objects through an 1000+ frame image
I have 2 tables, tracklist and track , where tracklist has many tracks .
I have a many-to-many relationship like this: A user has_many organizations through affiliations and
Let's say I have a schema where User has many Alerts (many to many).
I'm a long-time user of the DevExpress XPO library. It has many great features,
Is there an easy way to track how many pages a user on your
Ok I want to keep track of how many times my iPhone App has
Here are my models: **Resource** has_many :users, :through => :kits has_many :kits **User** has_many
I have this: class User < ActiveRecord::Base has_many :serials has_many :sites, :through => :series
I have been using touches began to track as many as 8 touches, and

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.