Welcome to day 5 our halfway point
we’re dangerously close to learning another core concept about applications— Databases
- Data model
- Demo API
- Lab time
before that I want to review relational db design that we discusses in weeks 1, 2 which probably feel like a long time ago, and do some data modeling and draw ERDs (entity relationship diagrams)
Remember in relational db design there are two main types of relationships between tables—
- one to many
- many to many we can determine which one is which by asking the two yes or no questions—
- Can one X be associated to multiple Y? (Yes or no)
- Can one Y be associated to multiple X? (Yes or no)
If both answers are Yes, we need a Join table with two foreign key columns If one answer is no, then the table you said "no" to gets a foreign key column for the other table.
So to practice this again, we’ll be taking a look at a simple prototype app: Offer Up
find the assignment on Canvas—
Your job: come up with the data model for this app.
- What are the main tables?
- What are the required columns in those tables?
- What are the relationships between tables?
Once you think you’ve identified the right tables, it can be helpful to start with a spreadsheet and fill out example rows in those tables based on data you see in the target to help validate your understanding
ultimately, your job is to create a ERD of the tables with the connections SUBMIT THE URL OF YOUR ERD
multiple correct solutions * solutions that can support all the features in the app * solutions that make our lives easier when we need to write the code still also incorrect solutions,
demo stuff, go over one solution look at example solution
number of tables the same or different? 5 or less
So as a helpful teammate, I can try to find gaps in this design before you start to actually write the code to implement this data model
- talk through the user flow
- different abilities users have
- sign in
- list product
- filter by category
- message user
- different abilities users have
I promised to go over the translate and text message API portion of omnicalc-2,
- bin/server
- see /add form
- street to coords works
- go to controller
- read address from user
- open URL for Google API that we found by doing research
- save the page as a String
- parse the JSON
- navigate
All this work reading the google maps API, it could be useful to be able to use this in other parts of the application
It would really suck if we had to copy paste, all this code it would work
but would be even better, is if I make a Ruby method that just does all this work for me, so I don’t need to remember or copy/paste.
Then I can do a .street_to_coords
method and get back the lat and lng
So let’s do that. I’ll make a file in the models folder, this is where we define our Ruby classes
Rails loads these files automatically
- geocoder.rb
- open
rails console
see that I can use it.- I can’t use just anything,
Potato
doesn’t work
- I can’t use just anything,
- now I can define an instance method, like we did in the our own classes chapter
- method arguments
- copy over this code from controller
- now in the console I can use class and run the method to get back the lat and lng
- this means, anytime in the future if I have a new team of developers work on this project, they don’t need to know the internals of how this method works, they just need to provide an argument and get back lat and lng.
- don’t need to know which URL to use for the Google Map API
- how to parse the JSON and navigate the Hash
- this type of operation is fairly common in applications, even in Offer Up, you saw the location of the listing had the coordinates
- What would be /even/ better is that for the most commonly used APIs developers have already done this work and wrapped it in a gem and put it on GitHub
- so anyone who wants to use the Google maps API just needs to add the class to their project and use the method
- sometimes the company themselves write this code to make it as easy as possible to use their API
- This approach is what we’ll use for the other two APIs
- they’re a little bit more complicated than the GMAPs API
Routes
- translate routes,
- form works
- but it doesn’t translate
- see the params hash in server
- add to controller as comment
- It’d be nice to do
Translator.translate(text)
and it just work. - We can do this by adding a google gem
- bundle and restart server
- open console
- copy paste into controller
- if you can find a gem, you are usually in good shape for interacting with the API
same process
- twilio’s is a company that has an incredibly massive APIs dealing with telephony, sending messages, voice calls, video calls, automated phone trees.
- they’re written a gem that we can use to send text messages with just one method call
- unlike GMAPs API, where we just read data from Google database
- this API actually creates a record in Twilio’s DB
- how to use?
- add to gemfile
- restart server
- add credentials
- craft method argument
- call method -> profit!
- It’s that easy
I’ll leave it there, you should have all the tools to complete this if you want
- There’s the readings to refer to
- There’s also the solutions you can find on /git
Any questions on this?
I want to leave time for you all to catch up on any old assignments and start on the homework.
Lab time: HW - databases, with a video headphones you can take and keep if you need them.
Optional assignment, if you want more practice with forms