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

The Archive Base Latest Questions

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

Suppose I have a User , Post and a Comment model, that allow a

  • 0

Suppose I have a User, Post and a Comment model, that allow a user to comment on a post. I’ve set up a polymorphic relationship like follows:

class User < ActiveRecord::Base
  has_many :comments
end

class GroupRun < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

class Comment < ActiveRecord::Base
  belongs_to :commentable, polymorphic: true
  belongs_to :user
end

I’d now like to allow a user to comment on another user, but am running into difficulty. I’d have thought I could do something along the lines of has_many :notes, :as_commentable but this doesn’t work.

Any advice?

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

    You have to add this to your User model:

    class User < ActiveRecord::Base
      has_many :comments
      has_many :comments_from_other_users, :as => :commentable, :class_name => 'Comment'
    end
    

    I know the name of the relation is awful but it is easily understandable and you can change it as you want.

    So you should have this after:

    current_user.comments.each do |comment|
      comment.user # author of the comment
      comment.commentable # object that you posted the comment on (GroupRun for instance)
    end
    current_user.comments_from_other_users.each do |comment|
      comment.user # author of the comment
      comment.commentable # user on which the comment is posted (should be equal here to current_user)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
Suppose you have a table like this: id user score It's a dump of
Suppose I have a table of users. Something like: ID integer, USER text, POSITION
I have one simple app that suppose to get information from user and send
Suppose, I want to have a link or a button that when user click
I was wondering how I'm suppose to associate my User, Post, and Comment models.
I have the models User, Post, Comment and Tag. User creates Post. Posts can
Suppose I have div and inside that div I have few form controls like
I have a simple query that is suppose to take user inputted text and
Suppose I have a user defined type: CREATE OR REPLACE TYPE TEST_TYPE AS OBJECT

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.