From: Phill Davies Date: 2008-08-12T05:04:28+09:00 Subject: Re: How to map this association Hi, I'm guessing this is a Rails question, in which case you should post it to the rails list (http://www.ruby-forum.com/forum/3). Anyway, since the question of ActiveRecord in ruby (sans rails) has been in the news so much lately, I guess it couldn't hurt to answer this here. If I understand the problem correctly, you have Users and Videos. Videos have a creator (1 creator to many videos) as well as owners (many owners to many videos). You were pretty close, except that the since 1 creator can have many videos you want the Video model to have a field e.g. user_id (or creator_id), rather than your User model having a video_id field. Phill Wenhan Zhou wrote: > Hi, This is the scenario > > A user can create videos and collect videos. > > I was coding the creation side of the videos so I was using a belongs_to > and a has_many. > > Then when I came to the part of collecting the videos, I am stumped with > how to implement the association. > > Normally I would just use a table with user_id and video_id. > But in this case, this would conflict with my earlier code. > > so the question is this: how do I implement an association for the > scenario belong. > > A user can create multiple video that are shown on his profile page > (1 to Many) > user.rb :has_many :videos > video.rb :belongs_to :user > user table has a coloum called video_id > > A user can also collect a video. This list is shown as videos favorited > by this user(Each video can also be favourite by many people) > (many to many) > user.rb : has_and_belongs_to_many :videos > video.rb :has_and_belongs_to_many :users > created a new table called user_video with user_id & video_id > > Since the 2 associations are for exactly the same models, > what happens when I call @user.videos > will I > 1) Get the list of videos created by this user > 2) Get the list of videos collected by this user > 3) the system goes bonkers :( > > Is this the wrong way to implement this? -- Posted via http://www.ruby-forum.com/.