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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:02:50+00:00 2026-05-17T20:02:50+00:00

For example I have a comments models, and I have a post model, but

  • 0

For example I have a comments models, and I have a post model, but comments can comment on other comments.

So it seems I need a join table I’ll call commentables. To create this, do I really need to create a commentable table with a post_id and a comment_id ?

Or can I do something like this without one :

has_many            :comments,
                    :through => :commentables,
                    :source => :post

Not really sure what’s the best way to accomplish this. I’m a huge newbie.

  • 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-17T20:02:51+00:00Added an answer on May 17, 2026 at 8:02 pm

    No, you shouldn’t need a join table in this case. Join tables are for has_and_belongs_to_many relationships, and in this case, you don’t need to have one of those (comments can’t belong to many posts, can they?).

    You’ve got two options to go about this. The first is to create a polymorphic relationship:

    class Post < ActiveRecord::Base
      has_many :comments, :as => :parent
    end
    
    class Comment < ActiveRecord::Base
      belongs_to :parent, :polymorphic => true
      has_many   :children, :class_name => 'Comment', :as => :parent # We need to point this relationship to the Comment model, otherwise Rails will look for a 'Child' model
    end
    

    This will allow a comment to either belong to a post, or another comment. Comment.last.parent will return either a Post or a Comment record.

    The second option is to make all comments belong to a specific post, but have their own parent-child relationship:

    class Post < ActiveRecord::Base
      has_many :comments
    end
    
    class Comment < ActiveRecord::Base
      belongs_to :post
      belongs_to :parent, :class_name => 'Comment'   # We need to point this relationship to the Comment model, otherwise Rails will look for a 'Parent' model
      has_many   :children, :class_name => 'Comment' # We need to point this relationship to the Comment model, otherwise Rails will look for a 'Child' model
    end
    

    This way, your comments will always belong to a post, as well as the potential to belong to another comment.

    If you’re planning to nest comments (at least more than one level), I would suggest the second option. This would allow you to fetch all comments for a specific post in one query (instead of having to find children for each comment), and you could sort the comments in the application before rendering them. But either way should work.

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

Sidebar

Related Questions

For example, I have a model Posts and Comments. Post.objects.annotate(Count('comment')) I am making such
For example I have 2 models that Topic and Comment . 1 Topic can
I have the models User, Post, Comment and Tag. User creates Post. Posts can
I have a Post, Comment and Vote model Each time an instance of Vote
I have 3 model, User, Post, Comment with definition like below class Post <
Example: I have a view controller and get rid of it. But there's still
I have a rails 3.1 app with the following Models: class Post < ActiveRecord::Base
I have multiple models which relate back to a single model. On save of
This example shows how to create a form for an associated model 'Comment', where
In my application, I have an user model/controller. An user can have multiple videos,

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.