- Define a many to many relationship.
- Demonstrate building a many to many relationship in Ruby.
- Come up with some real world examples of a many to many relationship.
- 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 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?
Slack to me:
- 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?
- Pretend you have a many to many relationship set up between
dogs
andparks
so the join model isdog_parks
. Give an example of how you would instantiate a newdog_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
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:
-
Customer#accounts
- Make a method on the
customer
class that returns an array of all accounts that belong to the specific customer.
- Make a method on the
-
Customer#banks
- Make a method that returns a
uniq
array of all the bank names an customer has.
- Make a method that returns a
-
Bank#accounts
- Method should return all accounts that belong to the specified bank.
-
Bank#customers
- Method should return an array of all customers of the specified bank.
-
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.
-
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.
-
Bank#oldest_account
- Method returns the oldest account associated with the specified bank.
-
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."
- (note the dot instead of the
-
Customer.average_number_of_accounts
- Method should return the average number of accounts all customers have.
-
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.
-
Bank#average_customer_age
- Method should return the average age of all customers for a specific bank.
-
Bank.most_branches
- Should return the bank with the most number of branches.
- OO My Pets
- OO Kickstarter
- OO Flatiron Mifflin
- OO Animal Zoo
- Look at file structure, talk about environment file talk about what it does
- create gemfile using bundle init
- can hardcode gems OR add using terminal command, bundle add then name of gem -bundle add pry require_all
- 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
- bring in all models and gems to environment.rb
- 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