Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

Feature issue implementation

I have an application that i'm working on , it's a video streaming app , the feature i'm trying to implement is playlist(collection of videos for the user to see with a unique name ).

in my associations :

user => has_many playlists, video => belongs_to playlists, playlist => belongs_to user

ps. in my app , general user cannot create videos only admin .

after created a playlist , the user should be able to add a video to the playlist via a button . if i'm correct the playlist should be an array .and there's will a function to add _to _playlist that will retrieve the video by video_id and then push it into the array and return the final playlist.

my main issue is, i'm pretty confused on how to do the implementation.any help will be gladly appreciate.

2 Answers

I think you're generally on the right track! Here's a run-through of the classes your describing and what they'd need to have in terms of relationships:

class User < ActiveRecord::Base
  has_many :playlists
end

This means that the playlists table with have an attribute named user_id representing what user owns the playlist.

class Video < ActiveRecord::Base
  belongs_to :playlist
end

This means that the videos table will include an attribute named playlist_id that shows what playlist the video belongs to. That'll mean that the video can only be on one playlist at any given time.

class Playlist < ActiveRecord::Base
  has_one :user
  has_many :videos
end

This means that the playlists table won't include any columns relating it to the other two tables, because they have columns on them that tell it what User and Videos it relates to.

So with that you'll need to do migrations for users, videos, and playlists that have those columns I mentioned above for each table included, along with any other columns you need for those objects in particular - like the name of the video or the name of the playlist, etc.

Hopefully that's a good starting walkthrough on that. Does that help?

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

That's a pretty good start , one mistake i make in the playlist class i have,

class Playlist < ActiveRecord::Base
belongs_to :user 
has_many   :videos
end

i was doing some tweaking in the rails console , i have come up with almost the same thing for the associations .but my I'm still confused .my process was to go and create a controller for the playlist and in the playlist model i will have a method ' add_to_playlist' that will retrieve the video id from the params and push it into the playlist and by that the playlist should be an array .That's my main roadblock.

So you're wanting to figure out how to write the add_to_playlist method?

Rails actually kind of already takes care of that for you. In your scenario you can add your video to your playlist with:

def add_to_playlist(video)
  playlist.videos << video
end

And since you've got the has_many association there it'll automatically set the correct id for the video. When the param comes in, I'd recommend loading it up in your controller with video = Video.find(params[:video_id]) and then passing it over to add_to_playlist. Of course, since Rails gives you the << operator you could just skip defining add_to_playlist and call playlist.videos << video directly in your controller.

Awesome! Definitely just keep posting if you need any more help. :grinning:

Obed Lorisson
seal-mask
.a{fill-rule:evenodd;}techdegree
Obed Lorisson
Front End Web Development Techdegree Student 6,151 Points

By the way you can check the application right here , it' just something i put together to learn more after the tutorial . Thanks for helping anyway