Justin Meltzer
Justin Meltzer

Reputation: 13548

How should I set up these polymorphic model associations?

I want to have a polymorphic relationship so that many videos, tags, and users can be posted to profiles. Profiles, videos, tags, and users are all separate resources. Would I need a separate model for this and combine some sort of has_many through association with polymorphism? I want profiles to have many videos, tags and users, but also videos, tags and users to have many profiles.

Upvotes: 0

Views: 191

Answers (3)

Spyros
Spyros

Reputation: 48606

You do not need polymorphic associations for this one. Instead you need has_many and has_many through associations.

Profiles have many videos and videos have many profiles is (in plain english) :

Profile has many videos through profile_videos
Video has many profiles through profile_videos

ProfileVideo belongs to profile
ProfileVideo belongs to video

Using that, you can now do either profile.videos to get a profile videos, or video.profiles to get a video's profiles.

Upvotes: 0

Mr. Black
Mr. Black

Reputation: 12082

I think this is help for you. http://railscasts.com/episodes/154-polymorphic-association

Upvotes: 0

rausch
rausch

Reputation: 3388

Why using polymorphism, when the has_and_belongs_to_many association seems to be the perfect match?

Upvotes: 1

Related Questions