Giter Site home page Giter Site logo

ruby-objects-has-many-lab-nyc-web-111918's Introduction

Ruby Objects Has Many Lab

Objectives

  • Build out the has many/belongs to relationship between two classes.
  • Build methods that use the has many/belongs to relationship between two classes.
  • Use a class variable to track data about a class.
  • Build a class method to expose data stored in a class variable.

Instructions

In this lab, we'll be dealing with two sets of classes. The Artist and Song class and the Author and Post class. An artist should have many songs and a song should belong to an artist. An author should have many posts and a post should belong to an author.

Because of the has many/belongs to relationship between your classes, the Artist tests, i.e. the tests in spec/artist_spec.rb, rely on some code in the Song class and vice versa. You will have to build out both the classes as you go. The same is true of the tests for the Author and Post classes. So, as you proceed through solving this lab, you will go back and forth between coding in Artist and Song and between coding in Author and Post.

We recommend starting out by getting some of the initial Artist tests passing and switching to write code in the Song class as directed by the test output. Then, once your Artist and Song tests are passing, do the same for Author and Post.

This lab is primarily test-driven. So run the test suite with the learn command to get started. Read the test output very carefully to help guide you through. First, read through the guidelines below and refer back to them as you complete the lab.

Building the has many/belongs to relationship

How can an artist have many songs and a song belong to an artist? An individual song will need to have a reference to the artist it belongs to, meaning it will need to have an artist=() and artist method. And an individual artist will need a method that returns a collection of all the songs that belong to that artist. Which means the song class will need to have a class variable that holds all instances of every song that's been created in an array. To access that array, the song class will also need a class method that returns the class variable holding those instances.

How can an author have many posts and a post belong to an author? An individual post will need to hold a reference to the author it belongs to, meaning it will need to have an author=() and author method. And an individual author will need a method that returns a collection of all the posts that belong to that author. Which means the post class will need to have a class variable that holds all instances of every post that's been created in an array. To access that array, the post class will also need a class method that returns the class variable holding those instances.

Who Knows About Who?

In order to have an artist find all of it's songs, the song class needs to know about all its song instances and a song instance needs to know about the artist class it belongs to.

The same goes for an author and a post. To do this, you'll need to create a class variable that keeps track of all its post instances and a post instance needs to know about the author class it belongs to.

The #add_song_by_name and #add_post_by_title Methods

The #add_song_by_name method should take in an argument of a name (or title), use that argument to create a new song (or post) and then associate the objects.

For add_post_by_title, you'll want to make sure the body of the method associates the newly created post with an author.

These methods don't need to return anything, they just create associations.

The #artist_name and #author_name Methods

Build the methods some_song.artist_name and some_post.author_name that return the names of the artist and author of the given song and post respectively.

These methods should use, or extend, the has many/belongs to relationship you're building out. If a song has an artist, you can call: some_song.artist and return an actual instance of the Artist class. Since every artist has a name, some_song.artist.name should return the name of the Artist instance associated with the given song. Your #artist_name method should utilize this relationship.

Note: We like our code to be robust, i.e. not easily breakable. Make sure your #artist_name and #author_name methods will not break if the given song or post does not have an artist or author. In this case, your methods should return nil.

The .song_count and .post_count Methods

You'll be required to write a class method that returns the total number of songs that have been created. You'll be required to write a similar method for the Author class. How can we get access to total number of songs from the artist class or the total number of posts from the author class?

View Has Many Object Lab on Learn.co and start learning to code for free.

ruby-objects-has-many-lab-nyc-web-111918's People

Contributors

maxwellbenton avatar minesja avatar sophiedebenedetto avatar drakeltheryuujin avatar victhevenot avatar annjohn avatar pletcher avatar dick-ward avatar gj avatar jmburges avatar marielfrank avatar asialindsay avatar aviflombaum avatar ga-be avatar

Watchers

 avatar James Cloos avatar Kevin McAlear avatar  avatar  avatar Belinda Black avatar  avatar Joe Cardarelli avatar Sam Birk avatar Sara Tibbetts avatar The Learn Team avatar  avatar  avatar Jaichitra (JC) Balakrishnan avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar  avatar Lore Dirick avatar Nicolas Marcora avatar Lisa Jiang avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.