Giter VIP home page Giter VIP logo

bank_party's Introduction

Many to Many in Ruby

Objectives

  1. Define a many to many relationship.
  2. Demonstrate building a many to many relationship in Ruby.

Warmup

  1. Come up with some real world examples of a many to many relationship.
  2. Try to think of something that connects these two things. Note: the connection between them should be one to many!!!!

What does this look like drawn out?

Let's Do It

Let's use what we know about one to many relationships to create 2 one-to-many relationships.

  • Work with your buddy to create a one to many relationship (just one of them).

  • We'll create the second one-to-many together.

That's it!!! We've now created a many-to-many relationship.

What can we do with this?

How can we go through our 'join model' to utilize our relationships?


Deliverables

Slack to me:

  1. When using a many-to-many relationship, which model/class holds the information to make the relationship work? In other words, which model knows about the other models?
  2. Pretend you have a many to many relationship set up between dogs and parks so the join model is dog_parks. Give an example of how you would instantiate a new dog_park instance.

Customer has many banks, Bank has many customers Account belongs to a bank and a customer

How to add a Gemfile!

  • pry
  • require_all

Getting Practice

You already have your class method .all (Account.all, AccountHolder.all, Bank.all) as well as methods to read all the attributes of instances in those classes. Use those to help you build out the following methods:

  1. Customer#accounts

    • Make a method on the customer class that returns an array of all accounts that belong to the specific customer.
  2. Customer#banks

    • Make a method that returns a uniq array of all the bank names an customer has.
  3. Bank#accounts

    • Method should return all accounts that belong to the specified bank.
  4. Bank#customers

    • Method should return an array of all customers of the specified bank.
  5. Customer#new_account(number, balance, bank)

    • Takes in 3 arguments, the account number, balance, and bank object and will create a new account associated with the customer instance the method was called on.
  6. Bank#new_account(number, balance, customer)

    • Takes in 3 arguments, the account number, nickname, and customer object and will create a new account associated with the bank instance the method was called on.
  7. Bank#oldest_account

    • Method returns the oldest account associated with the specified bank.
  8. Bank.most_customers

    • (note the dot instead of the # symbol above. What does that indicate?) Method should return the bank object that has the most customers. *For an extra challenge, have this method also return the number of customers. "________ Bank has _____ customers."
  9. Customer.average_number_of_accounts

    • Method should return the average number of accounts all customers have.
  10. Bank.customer_count

    • Method should return a hash where each name of the bank is the key and the values are the number of customers the bank has.
  11. Bank#average_customer_age

    • Method should return the average age of all customers for a specific bank.
  12. Bank.most_branches

    • Should return the bank with the most number of branches.

More Practice

  • OO My Pets
  • OO Kickstarter
  • OO Flatiron Mifflin
  • OO Animal Zoo
  1. Look at file structure, talk about environment file talk about what it does
  2. create gemfile using bundle init
  3. can hardcode gems OR add using terminal command, bundle add then name of gem -bundle add pry require_all
  4. run bundle to load all gems -may also see command as bundle install or bundle update -every time you clone a project from github, need to run bundle to install gems
  5. bring in all models and gems to environment.rb
  6. Look at models: account, bank, customer

Customer#accounts - # means need an INSTANCE method called accounts in the customer class -specific customer === instance

Bank.most_customers - . means need a CLASS method -all the banks of all the customers === class method

bank_party's People

Contributors

kathleenscriver avatar

Watchers

 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.