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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:42:15+00:00 2026-06-05T01:42:15+00:00

Let’s say I have a User model which has_many :posts , while Post has_many

  • 0

Let’s say I have a User model which has_many :posts, while Post has_many :comments.

If I do @user.posts.map {|post| post.comments}.flatten I’ll get all the comments on the user’s posts. Is there somewhere to configure the Post or Comment model to detect it’s being referenced in the context of a specific user and only return the specific user’s comments?

That is, @user.posts.map {|post| post.comments}.flatten and @posts.map {|post| post.comments}.flatten (assuming the same posts) wouldn’t return the same number of comments (assuming multiple users are commenting).

From a previous SO question’s answers, it sounds like I want some sort of nested has_many through. Is that correct? Is there any easy way in Rails 3 to detect the ‘source’?

  • 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-05T01:42:17+00:00Added an answer on June 5, 2026 at 1:42 am

    Updated answer:

    Here’s a method that’ll get a post’s author’s comments

    class Post < ActiveRecord::Base
      belongs_to :user   # So, there's a user_id attribute
      has_many :comments
    
      def authors_comments
        comments.where("user_id = ?", user_id)
      end
    end
    

    That should let you do:

    @user.posts.each { |post| puts post, post.authors_comments }
    

    it’s not as efficient as the other method, though; n posts will result in n SQL queries to get the comments. But it’s pretty close to what was described in the comment below.


    Original answer (for posterity)

    It ain’t the prettiest, but you can do something like

    class User < ActiveRecord::Base
      has_many :posts    # All the user's posts
      has_many :comments # All the user's comments on all posts
    
      # All comments made on any of user's posts
      has_many :replies, :through => :posts, :source => :comments
    
      def replies_to_self
        replies.where("comments.user_id = ?", id)
      end
    end
    

    Call @user.replies_to_self to get the users’ comments to his/her own posts

    You end up with SQL like this:

    SELECT
        "comments".*
    FROM
        "comments"
    INNER JOIN
        "posts"
    ON
        "comments"."post_id" = "posts"."id"
    WHERE
        "posts"."user_id" = X AND "comments"."user_id" = X
    

    (where X would be the user’s id)

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

Sidebar

Related Questions

Let's say I have an facebook application running using the JS SDK. First user
Let's say I have one class User, and it has a property of type
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have an string variable called *magic_string* which value is set to
Let's say we have this code: <form action='' method='POST' enctype='multipart/form-data'> <input type='file' name='userFile'><br> <input
Let's say I have some variables declared - but I don't know exactly which,
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let's say I have the following classes : public class MyProductCode { private String
Let's say I have a due date and a reminder timespan . How do

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.