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
First, you need to add a migration file to add columns in videos:
Then you can add polymorphic association in
Videomodel:And change your
Drummerlike this:Now you can use the polymorphic association: