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开发者_Python百科 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.
Why using polymorphism, when the has_and_belongs_to_many association seems to be the perfect match?
I think this is help for you. http://railscasts.com/episodes/154-polymorphic-association
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.
精彩评论