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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:52:15+00:00 2026-05-27T05:52:15+00:00

My scenario is that there are several different models which can have comments. Trying

  • 0

My scenario is that there are several different models which can have comments. Trying to figure out the relationships:

Post
  has_many :comments

Update
  has_many :comments

Comment
  belongs_to EITHER :post OR :update (but not both)????

Whats the proper way to set up the comment relationships? I want to be able to call Post.comments and Update.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-27T05:52:16+00:00Added an answer on May 27, 2026 at 5:52 am

    Smells like a polymorphic association:

    With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model.

    So you’d want something like this:

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

    You’d have to set up a few things in the database for this to work as well. See the Polymorphic Associations section of the Active Record Associations Guide for details on the columns you’ll need.

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

Sidebar

Related Questions

Scenario: I have several services that I want to be discovered by different clients.
I'm trying to write a Cucumber scenario that requires me to have a logged
Scenario: I have an application (C#) that expects a SQL database and login, which
If I have a java project that consists of several different types of files
I have a scenario where I have several classes that extend from a common
Scenario : There is this online questionaire that will be filled in by various
Are there any security exploits that could occur in this scenario: eval(repr(unsanitized_user_input), {__builtins__: None},
It seems to me that there are two scenarios in which to use JOINs:
Scenario I am building a database that contains a series of different tables. These
Here is the scenario that I have. I have a cvs repository in one

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.