Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active October 1, 2018 22:42
Show Gist options
  • Save harrisonmalone/26c5f6540d473f89f787ad3ba6e0cc3b to your computer and use it in GitHub Desktop.
Save harrisonmalone/26c5f6540d473f89f787ad3ba6e0cc3b to your computer and use it in GitHub Desktop.

Takeaways from models day one

Things to think about from today: we’ve been learning about databases and how they connect with Ruby. Basically, the way we connect them is with a Model: the M from MVC (Model, View, Controller).

But, what is a database?

It's something that stores data, but it's also more than this. Something that stores data could be: an array, a hash, a linked list, or any data structure - so just saying a database stores data is not hugely helpful.

A database needs to solve two problems:

  1. Store data and get it anytime we want.
  2. Store data in an organized and structured way, so we can get it easily.

We could store the data in a notepad? Just put all the information inside it separated by commas, save the txt file, and done. We can open it and get all the data that we need and we can store data and get it anytime… but it’s not well-structured in the file. We need to store and get data in an organized and well-structured form.

What about putting all the data in tables? This way we could have the table’s header (columns name: first_name, last_name, address, etc) containing values that we’ll store. For example, if we want to store the string “Gretchen” (the value), it’ll be stored in the “first_name” column.

  • Table: let’s call it User
  • Columns: first_name, last_name, address, etc
  • Rows: in this case, our row can be a person with, for example, first_name “Gretchen” and last_name “Scott,” address “123 I’m not telling,” etc.
  • Fields: all data stored in the database.

And now we have a well-structured way to store data: in a table!

We can use SQL language to manipulate the data.

  1. GET: If we want to get all data (user) from Users table, we need to select it from that table.
SELECT * FROM Users;
SELECT first_name, last_name, age FROM Users;
  1. DELETE: we want to delete all data from our Users table.
DELETE * FROM Users;

It’s unusual to delete all data from a table. Usually you would use a condition to delete, like “I want to delete all users under 40 years old.”

  1. INSERT: we will insert/store data into the table.
INSERT INTO Users VALUES (‘Matt’, ‘McKenzie’, 'My Address 123');

Relationship between tables

We know how to execute queries (with or without conditions). Let’s understand how the tables' relationships work.

  • One to One (1–1): it’s about a relationship between two tables in which one can only belong with the other. e.g. A person has one passport and that passport belongs to that specific person. So in this example, we have table People table Passports and a 1–1 relationship.
  • One to Many (1-n): it’s about a relationship between two tables in which a record from one table can reference many records from another. e.g. Imagine an e-commerce platform: users, orders, products, payments, etc. A user can have many orders, and each order belongs to that specific user. So in this example, we have table Users, table Orders, and a 1-n relationship.
  • Many to Many (n-n): it’s about a relationship between two tables in which a record from one table can reference many records from another. And a record from another can also reference many records from the one. e.g. We have again the e-commerce platform: we divide products into categories. A category has many products (category Technology has many products like cell phones, notebooks, etc) and a product can belong to many categories (product Cellphone belongs to the Technology and Electronics Categories). So in this example, we have table Products, table Categories, and an n-n relationship. We would often use a join table in this situation.
id, category_id, product_id  
1      1             4
2      1             6
3      2             1
4      3             2
5      3             4
6      3             2

Important Point

First of all: Ruby is Ruby. The Database is Database. A User model can represent a Users table. But the model isn’t the table.

  • In the database, we have tables and rows.
  • In Ruby, we have models (classes) and objects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment