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

  • Home
  • SEARCH
  • 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 9308005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:31:21+00:00 2026-06-19T00:31:21+00:00

I need to link Comments to a Post. However the Comment could be (user

  • 0

I need to link Comments to a Post. However the Comment could be (user generated) a simple text, (system generated) a link or an (system generated) image.

At first they all shared the same attributes. So I just needed to create a category attribute, and do different stuff with the text attribute based on that category.

example:

class Comment < ActiveRecord::Base
  belongs_to :post
  belongs_to :author, :class_name => "User"

  CATEGORY_POST = "post"
  CATEGORY_IMAGE = "image"
  CATEGORY_LINK = "link"

  validates :text, :author, :category, :post, :presence => true
  validates_inclusion_of :category, :in => [CATEGORY_POST, CATEGORY_IMAGE, CATEGORY_LINK]

  attr_accessible :author, :text, :category, :post

  def is_post?
    self.category == CATEGORY_POST
  end

  def is_link?
    self.category == CATEGORY_LINK
  end

  def is_image?
    self.category == CATEGORY_IMAGE
  end

end

However this wil not suffice now, because I doesn’t feel clean to dump every value in a generic “text” property. So I was thinking about create a polymorphic model (and if needed in a factory pattern). But when I googled about polymorphic models, I get examples like a Comment on a Post, but the same Comment on a Page, kind of relations. Is my understanding of polymorphic different (a model that acts different in different situations, compared to a model that acts the same under different scopes)?

So how would I set up this kind of relationship?

I was thinking of (and please correct me)

 Post
    id

 Comment
    id
    post_id
    category (a enum/string or integer)
    type_id (references either PostComment, LinkComment or ImageComment based on category)
    author_id

 PostComment
    id
    text

 LinkComment
    id
    link

 ImageComment
    id
    path

 User (aka Author)
    id
    name

But I have no clue how to setup the model so that I can call post.comments (or author.comments) to get all comments. A nice to have would be that the creation of a comment would be through comment and not link/image/postcomment (comment acting as the factory)

My main question is, how to setup up the activerecord models, so the relations stay intact (a author has comments and a post has comments. Comments being either a Link, Image or Postcomment)

  • 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-19T00:31:23+00:00Added an answer on June 19, 2026 at 12:31 am

    I’m going to answer only your main question, the model setup. Given the columns and tables you used in your question, with the exception of Comment, you can use the following setup.

     # comment.rb
     # change category to category_type
     # change type_id to category_id
     class Comment < ActiveRecord::Base
       belongs_to :category, polymorphic: true
       belongs_to :post
       belongs_to :author, class_name: 'User'
     end
    
     class PostComment < ActiveRecord::Base
       has_one :comment, as: :category
     end
    
     class LinkComment < ActiveRecord::Base
       has_one :comment, as: :category
     end
    
     class ImageComment < ActiveRecord::Base
       has_one :comment, as: :category
     end
    

    with that setup, you can do the following.

     >> post = Post.first
     >> comments = post.comments
     >> comments.each do |comment|
          case comment.category_type
          when 'ImageComment'
            puts comment.category.path
          when 'LinkComment'
            puts comment.category.link
          when 'PostComment'
            puts comment.category.text
          end
        end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to display link after each comment, when you click that link, a
I need a link in django template which turns into logout if user is
I have a DLL generated from Visual Studio 2010, but I need to link
File: /home/USER/DIR/a http://www.here.is.a.hyper.link.net/ /home/USER/DIR/b http://www.here.is.another.hyper.link.net/ Need to remove all the odd lines in this
How would i use <% link_to (post) do %> <%= pluralize(post.comments.size,'comment') %> <% end
I have some code that when a user clicks on a link Add Comment,
In my application a user can post comments against either a post or a
<?php include('db_connect.php'); ?><head> <link rel=stylesheet type=text/css href=comment-style.css media=screen /> <script type=text/javascript src=../jquery.tablesorter/jquery-latest.js></script> <script type=text/javascript
This is one => echo $this->Html->link('Edit', array('controller'=>'comments','action'=>'edit',$comment['Comment']['id'])); This is another one in form =>
Please i need your help with my script. I'm trying to post comments to

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.