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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:17:08+00:00 2026-05-16T04:17:08+00:00

I have this working to a degree, but I am looking for some input

  • 0

I have this working to a degree, but I am looking for some input on how to query for the siblings in a one to many relationship to see if there is a more elegant way of accomplishing this.

Consider the following classes

class Post < ActiveRecord::Base
  has_many :post_categories
  has_many :categories, :through => :post_categories
end


class Category < ActiveRecord::Base
  has_many :post_categories
  has_many :posts, :through => :post_categories
end

A post by definition can have multiple categories, what I would need this for is to show a “related posts” area on the site. Like I mentioned previously I do have a working version which is to simply do the following:

Post.find(id, :include => {:categories => :posts})

Looking at the logs the application then has to do five queries to get the end data that I am looking for.

Any thoughts are appreciated!

  • 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-05-16T04:17:09+00:00Added an answer on May 16, 2026 at 4:17 am

    The only problem I see you having with what you have already is that you probably don’t want to return all posts which share a category with a post.

    @post = Post.find params[:id]
    @related_posts = Posts.find(:all, :joins => :post_categories,
                                  :select => "posts.*, count(post_categories) post_category_count", 
                                  :conditions => {:post_categories => {:category => @post.categories}}, 
                                  :group => "posts.id", :order => "post_category_count desc")
    

    This will return the most relevant posts first, ie. those which have the most shared categories, and you can either add a limit or paginate in order to limit results returned.

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

Sidebar

Related Questions

There are a couple of articles on this, and I have this working...but I
I have this legacy code that I am working with and there is code
I have this working fine from Linux command line: wkhtmltopdf entry.html output.pdf But the
I have this working definition: IDENTIFIER [a-zA-Z][a-zA-Z0-9]* I don't want to keep repeating the
I have this classic ASP site which has been working fine until we updated
I have this piece of code (summarized)... AnsiString working(AnsiString format,...) { va_list argptr; AnsiString
I'm working on an ASP.NET page, using VB.NET and I have this hierarchy: Page
I have been working on this for the greater part of the day and
I'm still working with this huge list of URLs, all the help I have
I have been working on Flex for last couple of months and as this

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.