Giter Site home page Giter Site logo

ruby-objects-has-many-through-lab-v-000's Introduction

Ruby Object Relations: Has-Many-Through Lab

Objectives

  1. Write classes that are related to each other via the "has-many-through" relationship.
  2. Write methods that use the "has-many-through" relationship to deliver information on related objects.

Overview

In this lab, we'll be dealing with two sets of three classes.

We have a Song, Artist and Genre class. Songs belong to an artist and belong to a genre. A genre has many songs and an artist has many songs. An artist has many genres through it's songs and a genre has many artists through it's songs.

We also have an Appointment, Doctor and Patient class. Appointments belong to a patient and to a doctor. A doctor has many appointments and a patient has many appointments. A doctor has many patients through it's appointments and a patient has many doctor's through it's appointments.

Instructions

Start with the Artist/Song/Genre domain.

The Artist model:

  • An artist is initialized with a name and an empty @songs array.
  • The Artist class needs an instance method, #add_song, that takes in an argument of a song and adds that song to the artist's collection of songs. The #add_song method should also tell the song that it belongs to that artist.
  • The Artist class needs an instance method, #songs, that returns the @songs array.
  • The Artist class needs an instance method, #genres that iterates over that artist's songs and collects the genre of each song.

The Song model:

  • A song should be initialized with a name and a genre. The initialize method should associate that song to the genre passed in as an argument. How can you associate a song to a genre? A song needs a "genre" attribute. Try creating an attr_accessor for genre. Genres also need to know about songs. You'll need to create a Genre#add_song method. Let's come back to this in a minute.

The Genre model:

  • A genre should be initialized with a name and an empty @songs array.
  • The Genre class needs an instance method, #add_song, that takes in an argument of a song and adds that song to the genre's collection of songs. Now you can use the Genre#add_song method in the Song class' initialize method to tell the genre that it owns the given song.
  • The Genre class needs an instance method, #songs, that returns the @songs array.
  • The Genre class needs an instance method, #artists, that iterates over the genre's collection of songs and collects the artist that owns each song.

Now let's move on to our Doctor/Appointment/Patient domain model.

The Doctor model:

  • A doctor should be initialized with a name and an empty @appointments array.
  • The Doctor class needs an instance method, #add_appointment, that takes in an instance of the Appointment class and adds that appointment to the doctor's @appointments array. The method should also tell that appointment that it belongs to that doctor.
  • The Doctor class needs an instance method, #appointments, that returns the @appointments array.
  • The Doctor class needs an instance method, #patients, that iterates over that doctor's appointments and collects the patient that belongs to each appointment.

The Appointment model:

  • An appointment should be initialized with a date (as a string, like "Monday, August 1st"), and a doctor. The #initialize method should tell the new appointment that it belongs to the doctor passed in as an argument and tell the doctor that it now has this appointment in its collection of appointments. Use the Doctor#add_appointment method here.

The Patient model:

  • A patient is instantiated with a name and an empty @appointments array.
  • The Patient class needs an instance method, #add_appointment, that takes in an argument of an appointment and adds that appointment to the @appointments array. This method should also tell the appointment that it belongs to this patient.
  • The Patient class needs an instance method, #appointments, that returns the content of the @appointments array.
  • The Patient class needs an instance method, #doctors, that iterates over that patient's appointments and collects the doctor that belongs to each appointment.

View this lesson on Learn.co

ruby-objects-has-many-through-lab-v-000's People

Contributors

sophiedebenedetto avatar annjohn avatar jmburges avatar aviflombaum avatar rocketneck avatar

Watchers

James Cloos 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.