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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:13:12+00:00 2026-06-11T09:13:12+00:00

I have an app with users, relationships, posts, likes. My models are: class User

  • 0

I have an app with users, relationships, posts, likes.
My models are:

class User
  has_many :posts
  has_many :likes
  has_many :relationships, :foreign_key => "follower_id", :dependent => :destroy
end

class Post
  belongs_to :user
  has_many :likes  
end

class Like
  belongs_to :user
  belongs_to :post
end

class Relationship
  belongs_to :follower, :class_name => "User"
  belongs_to :followed, :class_name => "User"
end

So I want to find at least 100 users who likes my current post:

friends = User.find(user.followers).likes.where(:post => @post, :limit => 100)

It’s a simple but not optimized query if there are a lot of users, posts, likes, etc. in DB.
How can I optimize the query (or models) to increase speed and to decrease query’s time execution?

  • 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-11T09:13:14+00:00Added an answer on June 11, 2026 at 9:13 am

    Well, the first thing to do is to make sure you have proper indexes on all your tables.

    So there should be an index on all the primary and foreign keys used to join the tables. Then of course you want indexes on any fields in these tables that your might use for sorts or filters.

    Outside of that, I don’t really see any problem with your database schema.

    If, however you want look at non-relational database, a lot of developers are using NoSQL storage for problems such as these where you have one main post, but there may be any number of likes, comments, etc. on it. It is really easy to maintain a single NoSQL document entry in say JSON that contains the entire tree structure for a single post, rather than having to assemble this information from disparate table in a relational DB structure.

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

Sidebar

Related Questions

In my app, I have three models: Markets, Deals, and Users. Relationships are pretty
I have this code App.Model('users').find(function (err, users) { users.forEach(function(user) { console.log(user.username); }); }); //make
I have a simple web app with users and posts. I have an index
I have some rails app. For example I have next models: User , Post
i have trouble to user uniqness couples in my app... I have users witch
I have a rails app with two models, visits and users. A visit has
I have an app where users can change a toggle value between true and
I have an app where users can sign up with Facebook, in which case
I have an app that allows users to upload large data files and then
I have an app in which users can follow law firms I have 3

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.