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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:45:01+00:00 2026-05-17T19:45:01+00:00

I have users, posts and comments. User can post only one comment to each

  • 0

I have users, posts and comments. User can post only one comment to each post.

class User < ActiveRecord::Base
  has_many :posts
  has_many :comments
end

class Post < ActiveRecord::Base
  has_many :comments
  belongs_to :user
end

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :post
end

On userpage (http://host/users/1 for example) I want to show all posts where the given user has commented. Each post then will have all other comments.

I can do something like this in my User controller:

def show
  @user = User.find(params[:user_id])
  @posts = []
  user.comments.each {|comment| @posts << comment.post}
end

This way I will find User, then all his comments, then corresponding post to each comment, and then (in my view) for each post I will render post.comments. I’m totally new in Rails, so I can do this =) But I think it’s somehow bad and there is a better way to do this, maybe I should use scopes or named_scopes (don’t know yet what this is, but looks scary).

So can you point me out to the right direction here?

  • 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-17T19:45:01+00:00Added an answer on May 17, 2026 at 7:45 pm

    You could define an association which retrieves all the posts with comments in a single query. Keeping it in the model reduces the complexity of your controllers, enables you to reuse the association and makes it easier to unit test.

    class User < ActiveRecord::Base
      has_many :posts_with_comments, :through => :comments, :source => :post
      # ...
    end
    

    :through is an option for has_many to specify a join table through which to perform the query. We need to specify the :source as Rails wouldn’t be able to infer the source from :post_with_comments.

    Lastly, update your controller to use the association.

    def show
      @user  = User.find(params[:user_id])
      @posts = @user.posts_with_comments
    end
    

    To understand more about :through and :source take a look at the documentation.

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

Sidebar

Related Questions

I have a site where users can post stuff (as in forums, comments, etc)
So I have a User model that :has_many other models like Documents, Videos, Posts
I have a view user control that can post form. This control can be
I have a form that uses jQuery to submit an ajax post and it
I have users who are using secure-wave security. Evidently it is some sort of
We frequently have users that create multiple accounts and then end up storing the
Here's the scenario: We have users login on a secure connection. Could an unathorized
What is the best way to determine available bandwidth in .NET? We have users
In a PHP application I am writing, I would like to have users enter
Our users have created MS-Excel spreadsheets which over time have evolved into fairly complex

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.