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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:07:24+00:00 2026-05-18T20:07:24+00:00

for instance, I have 2 model Drummer and Video If I use one-to-many association

  • 0

for instance, I have 2 model Drummer and Video

If I use one-to-many association drummer has_many videos, and video belongs_to drummer,

So I can use the method in both side: video.drummers or drummer.videos

But when I use polymorphic association, which I think the right to for this, because, there will be a lot model have video, it makes sense make the video model polymorphic

I can use drummer.videos,
But I don’t know how to get which drummer the video belongs to

video.drummer doesn’t work,

anybody could tell me how to do this, and what’s other methods I can use after I set up the association.

Thanks

  • 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-18T20:07:25+00:00Added an answer on May 18, 2026 at 8:07 pm

    First, you need to add a migration file to add columns in videos:

    class AddPolymorphicReferenceToVideos < ActiveRecord::Migration
      def self.up
        add_column :videos, :video_holder_id, :integer
        add_column :videos, :video_holder_type, :string
        add_index :videos, :video_holder_id
        add_index :videos, :video_holder_type
      end
    
      def self.down
        # just remove indexes and columns above
      end
    end
    

    Then you can add polymorphic association in Video model:

    class Video < ActiveRecord::Base
      belongs_to :video_holder, :polymorphic => true
    end
    

    And change your Drummer like this:

    class Drummer < ActiveRecord::Base
      has_many :videos, :as => :video_holder
    end
    

    Now you can use the polymorphic association:

    drummer.videos  # => an videos array
    video.video_holder  # => a model holds the video
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for instance, I have 2 model Drummer and Video If I use one-to-many association
I have a model book with a one to many relation with authors. I
I have a model with many validations that can be grouped based on various
I have a model instance already, it's a basic POJO, how can I populate
I have a model like the one below. When an instance is created, I
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
For instance, let's say I have a User model. Users have things like logins,
I have a form that edits an instance of my model. I would like
I have trouble keeping track of the properties of a Model instance. For example
I have a django application using multiple databases. Given an instance of a model,

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.