W
Wenhan Zhou
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?
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?