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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:05:48+00:00 2026-06-10T17:05:48+00:00

First, I have a polymorphic association setup because comments can belong to any object(in

  • 0

First, I have a polymorphic association setup because comments can belong to any object(in my case post and articles).

I want to be able to say:

u = user.first
u.comments #This will list all comments from a user
u.comments.where(:commentable_type => "Post")  

This above line doesn’t work. it generates a sql:
SELECT “comments”.* FROM “comments” WHERE “comments”.”commentable_id” = 1 AND “comments”.”commentable_type” = ‘User’ AND “comments”.”commentable_type” = ‘Post’

obviously this would return an empty list because a comment can’t belong to 2 types. I also want to be able to say:

f = Food.first
f.comments.first.user #give me the user that posted the first comment

Here’s my basic model… any tips on changing this?

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

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

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

class User < ActiveRecord::Base
    has_many :comments, :as => :commentable
end
  • 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-10T17:05:50+00:00Added an answer on June 10, 2026 at 5:05 pm

    I think you should review your comment model, it should look like:

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

    So you’d have two relationships, the first will point to who posted the comment, the other will point to the object being commented.

    Comment.first.user # this will return the user
    
    Comment.first.commentable # this will return the object which the comment was attached (Post, Article or ?Food?)
    

    Dont forget the migrations if you wish to try this approach.

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

Sidebar

Related Questions

I was wondering if I can have a polymorphic association and also a presence
First I have install Mac 10.6.3 on my system now I want to install
Some setup background first: I have a cronjob which runs a PHP file called
If I have something like this: class Post < ActiveRecord::Base has_many :comments, :as =>
I am working on my first polymorphic association relationship and I am having trouble
To access a remote database, I first have to use a program called Shrew
In order to bring up the menu for a DataGridViewComboBoxCell, I first have to
First I have a Listbox and set the DataSource to a MyObjectCollection MyObjectCollection implements
First I have a PHP file that gets data and file from a HTML
I have first: select url from urls where site = '$value' order by url

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.