I have been really struggling with learning to manipulate Object Relationships in Ruby. Wrapping my mind around the logic that follows from one class to another remains tricky through as many number of times as I have practiced it. Still, I know it is super important and will be used throughout the rest of the course at Flatiron School and beyond. So I have continued to find new ways of practicing building classes and mapping these relationships. This week I decided to create my own project by modeling the domain of Youtube.
The website used by millions of people to replace the void left in our lives after Americas Funniest Home Videos went off the air.
I began making the model using the three classes: Poster (someone who makes a video and posts it), Viewer, and Video. I initialized the Poster and Viewer with usernames and the Video with a title. I knew the Video would need to know who posted it and be able to find out who was watching it, so I added poster and viewer to the Video class. Then I could begin writing methods!
I wrote the basics, a method to collect all the Viewers, all the Videos, all the Posters. A method that allows the Poster to post a video and allows them to know who the viewers are of their videos.
But once I began writing a method that would tell which videos a user has watched, and then one to count how many times a user has watched each video (cause we all know we have those videos we can watch over and over...)
When trying to write these methods however, it seemed like something was missing. I just couldn't seem to get them to work with the setup how it was. so I re-examined my model and found that I was indeed missing something that could make everything else fit together. Instead of just using the Video class as the through class, I needed to add a Watch class to create instances of a viewer watching a video and collect those instances to be counted and examined in other class' methods. Once I created this fourth class I had a model unlike anything I'd worked with before. Could I really have a has-many-has-many-through model? Would the poster still be able to understand who the viewers are though that extra layer?
Its totally possible!
By using the Video and Watch classes both as through classes I was able to write methods that could relate to them all. The Poster was able to collect the usernames of the viewers.
The Video class has methods that can count the number of views for each video and collect the usernames of the viewers.
And the Viewer class is able to collect a list of all the watched videos. Produce a count of how many times each video has been watched and then output the viewer's favorite video based on the video that has been watched the most.
The hardest part of this model was writing these last three methods. all_watched_videos required looping through an instance twice in order to return only each video's title. The watch_count method required creating a new hash and placing each video into key-value pairs within that hash and then incrementing a key's value each time another instance of the same video was encountered in the all_watched_videos array. Then favorite_video required iterating over the hash from watch_count and putting the title, found in the key of the hash, which has the highest value.
If I were to continue working on this, I would like to add a comments class, and add methods where a viewer could leave a comment, and a Poster could respond to a comment.
This project may have become bigger and trickier than I expected, but it taught me a lot about object relationships.
No comments:
Post a Comment