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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:14:28+00:00 2026-05-30T14:14:28+00:00

I have several models that are commentable (article, post, etc.). At the moment each

  • 0

I have several models that are commentable (article, post, etc.). At the moment each commentable model contains the following association

has_many :comments, :as => :commentable

and the comment model contains:

belongs_to :commentable, :polymorphic => true

My commentable models share some similar characteristics, and I’d like them to be able to use a few of the same functions. However, I think MTI (multiple-table inheritance) might be overkill for the situation. Is it possible/acceptable for me to just create a base model class that they both inherit? i.e.:

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

class Commentable < ActiveRecord::Base
  has_many :comments, :as => :commentable
  validates_presence_of :body
  def some_function
    ...
  end
end

class Article < Commentable
  ...
end

class Post < 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-05-30T14:14:29+00:00Added an answer on May 30, 2026 at 2:14 pm

    You’re probably better off to create a Commentable module and then include that module.

    module Commentable
        def some_function
           ...
        end
    end
    
    class Article < ActiveRecord::Base
        has_many :comments, :as => :commentable
        validates_presence_of :body
    
        include Commentable
        ....
    end
    

    If you want to avoid duplicating the has_many and the validates_presence_of statements you could follow the acts_as pattern for your module.

    In that case you could do something like

    # lib/acts_as_commentable.rb
    module ActsAsCommentable
    
      extend ActiveSupport::Concern
    
      included do
      end
    
      module ClassMethods
        def acts_as_commentable
          has_many :comments, :as => :commentable
          validates_presence_of :body
        end
      end
    
      def some_method
        ...
      end
    
    end
    ActiveRecord::Base.send :include, ActsAsCommentable
    
    # app/models/article.rb
    class Article < ActiveRecord::Base
      acts_as_commentable
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several models: Email Letter Call All three belong to a model Campaign.
I have several similar Models (ContactEmail, ContactLetter, ContactPostalcard). Each instance (record) in, say, ContactEmail,
Django newbie here, I have several types of models, in each of them the
I have several models that includes nested classes and lists. Many of the class
I have several Rails Active Record Models: ContactEmails, ContactCalls, ContactPostalcards. Each one represent a
I have several models with fields that are integers. I also have hashes that
I have the following model and it's form: class Project(models.Model) class ProjectForm(forms.ModelForm) class Meta:
I have several models that are related to one another through a HABTM relationship.
Let's say you have several models that contain fields for address, postal code, province/country,
I've got a situation where I have a project with several models that have

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.