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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:39:45+00:00 2026-05-22T11:39:45+00:00

I have several tables, let’s say for example: articles, thoughts and pages and I

  • 0

I have several tables, let’s say for example: articles, thoughts and pages and I want to be able to add comments for each one of them.
Should I add comments tables for each on of the table (articles_comments,pages_comments…) or somehow do general comments table for all kinds of data (plz extend how to implement that in rails if you can)

I had an idea to do comments table that contain

[COMMENTS] ID, MODEL, MODEL_ID,
USER_ID, TEXT

while model contain the destination table(articles/posts…) and model_id the foreign id in the table, is it good solution?
the problem is that I have no idea how to implement such model in rails.

I also would like to know how Facebook implemented their posts db.
fb user can post any kind of data (question/status/poll/picture) and it’s just fit in the feeds table with current order and comments.

  • 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-22T11:39:46+00:00Added an answer on May 22, 2026 at 11:39 am

    create a general Comment model and make it polymorphic like this:

    class Comment < ActiveRecord::Base
      belongs_to :commentable, :polymorphic => true
    end
    
    class Article < ActiveRecord::Base
      has_many :comments, :as => :commentable
    end
    
    class Thought < ActiveRecord::Base
      has_many :comments, :as => :commentable
    end
    

    From an instance of the Article model, you can retrieve a collection of comments: @article.comments

    If you have an instance of the Comment model, you can get to its parent via @comment.commentable

    To make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface:

    class CreateComments < ActiveRecord::Migration
      def self.up
        create_table :comments do |t|
          t.text       :content
          t.references :user
          t.references :commentable, :polymorphic => true
          t.timestamps
        end
      end
    
      def self.down
        drop_table :comments
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several tables whose only unique data is a uniqueidentifier (a Guid) column.
This issue is driving me mad. I have several tables defined, and CRUD stored
I have several sources of tables with personal data, like this: SOURCE 1 ID,
I have created a Python module that creates and populates several SQLite tables. Now,
I have a long running insert transaction that inserts data into several related tables.
Let's say I have a table, category , which has 3 columns, id ,
I have an HTML table with several columns and I need to implement a
I have a SQL Mobile database with one table. It has several columns with
While trying to use LINQ to SQL I encountered several problems. I have table
I have several ASP:TextBox controls on a form (about 20). When the form loads,

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.