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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:53:32+00:00 2026-05-26T15:53:32+00:00

Imagine I have a model called Course and each course has_many Modules . However

  • 0

Imagine I have a model called Course and each course has_many Modules. However I want the modules to be a different range of types.

For instance, the Module model would be the parent class and would have two fields: title and description that will be common amongst all types of children.

Now I need Course to be able to have any number of Slideshow, Video, Image, Text instances, however they must be retrieved through Course#modules.

Consider that each child class type has its own attributes, like Video could have an url field, whereas Text could have contents, for instance.

What’s the right way to model this association?

  • 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-26T15:53:33+00:00Added an answer on May 26, 2026 at 3:53 pm

    I think there are 2 options (possibly combined):

    • Use polymorphic associations (see “Polymorphic Associations” for an example). Your migration should look like:

      create_table :courses do |t|
        t.references :modules, :polymorphic => {:default => 'Text'}
      end
      
    • Use Single-Table Inheritance: There your models are subclassed from a common base class, and this is realized on the database by only one table that contains all columns for all model classes.

    Have a look at “Alex Westholms Blog” about the 2, and the comparison. I think you should go with polymorphic associations, because your modules have a lot of different attributes.

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

Sidebar

Related Questions

In this simplified scenario, I have a model called Post Each post has a
Imagine I have a abstract FriendEvent model which has several different concrete implementations, ie.
I have a problem. Imagine this data model: [Person] table has: PersonId, Name1 [Tag]
Imagine I have String in C#: I Don’t see ya.. I want to remove
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into
I have a core data model with an entity called clients that is made
Imagine that I have a model that describes the printers that an office has.
So, imagine I have this model: class Car has_one :engine end and the engine
I have a Django model called Collection that represents a collection of items (CollectionItem).
Imagine I have a list of objects that implement an interface called ISummary The

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.