Latest / CyberCode Academy / Course 37 - Building Web Apps with Ruby On Rails | Episode 4: Mastering Data Modeling and Resource Relationships in Rails
Transcript
- 0:00Speaker 1: So back in twenty fourteen, there was this massive international
- 0:04Speaker 1: corporation that ended up losing like millions of dollars in
- 0:07Speaker 1: a single afternoon.
- 0:08Speaker 2: Oh yeah, I know exactly what you're talking about, the
- 0:10Speaker 2: ultimate night mercenario, right.
- 0:11Speaker 1: And the crazy thing is it wasn't a market crash,
- 0:14Speaker 1: It wasn't you know, a bad investment strategy or anything.
- 0:17Speaker 1: It was a complete system failure caused by a single
- 0:20Speaker 1: word spelled wrong in a database.
- 0:23Speaker 2: Millions of rows of data, all brought down by one typo.
- 0:26Speaker 1: Yeah exactly. Someone basically just typed a plain text name
- 0:30Speaker 1: in a field that was expecting something else, and the
- 0:33Speaker 1: entire math engine behind the company's software just it completely
- 0:37Speaker 1: folded in on itself.
- 0:38Speaker 2: Which is terrifying, honestly, because that usually happens when people
- 0:41Speaker 2: try to take the messy, infinite complexity of the real
- 0:46Speaker 2: world and just kind of cram it into a computer
- 0:48Speaker 2: without setting up rigid rules first, right.
- 0:50Speaker 1: Which brings us to our mission for today's deep dive.
- 0:54Speaker 1: We're going to talk about the invisible structures that prevent
- 0:56Speaker 1: that exact kind of catastrophe. So imagine for a second
- 1:00Speaker 1: that you are listener, want to build your own personal
- 1:03Speaker 1: financial tracking system.
- 1:04Speaker 2: Okay, I like where this is going.
- 1:06Speaker 1: Yeah, so you want to track historical stock prices. Say
- 1:08Speaker 1: you start with Tesla because you want to run some analysis,
- 1:12Speaker 1: but you know you're ambitious. You know you might want
- 1:14Speaker 1: to add Facebook or Google or literally any other company
- 1:18Speaker 1: down the line. Right, You need flexibility exactly, So how
- 1:21Speaker 1: do you translate that real world goal into a clean,
- 1:24Speaker 1: functioning digital system. Today we are driving deep into data modeling. Specifically,
- 1:31Speaker 1: we're going to take a conceptual idea and bring it
- 1:33Speaker 1: to life using a framework called Rubyon.
- 1:36Speaker 2: Rails, which is such an incredibly powerful tool for this,
- 1:39Speaker 2: it really is.
- 1:40Speaker 1: So we're going to break the world down into what
- 1:42Speaker 1: we call entities, link them all together, and seamlessly translate
- 1:46Speaker 1: them into a living database. By the end of this
- 1:49Speaker 1: deep dive, you're going to understand the hidden architecture powering
- 1:52Speaker 1: the apps you use every single day.
- 1:54Speaker 2: And honestly, the best part about this whole process is
- 1:57Speaker 2: that the very first step of building you know, atproof system,
- 2:00Speaker 2: it actually has absolutely nothing to do with writing.
- 2:03Speaker 1: Code, well really no code at all.
- 2:05Speaker 2: Zero code. Before you even touch a keyboard. You have
- 2:08Speaker 2: to look at your overarching goal, in this case, tracking stocks,
- 2:12Speaker 2: and you just start isolating the mounds in your story.
- 2:14Speaker 2: You have to identify the entities.
- 2:16Speaker 1: Okay, So an entity is just what a generic concept
- 2:19Speaker 1: that represents a real world thing.
- 2:21Speaker 2: Exactly, It's a generalized Now.
- 2:23Speaker 1: Okay, well, if I look at my financial tracker, my
- 2:26Speaker 1: first instinct is just to say, well, Tesla is the
- 2:29Speaker 1: main player here. So Tesla is my first entity.
- 2:32Speaker 2: And see that instinct right there? That is exactly what
- 2:36Speaker 2: backs early developers into a massive corner. Really, how so,
- 2:40Speaker 2: because Tesla is just one specific instance of a thing.
- 2:43Speaker 2: If you create an entity and actually name it Tesla,
- 2:46Speaker 2: you've hard coded your entire system to only ever understand
- 2:49Speaker 2: that one single business.
- 2:51Speaker 1: Oh I see right.
- 2:52Speaker 2: So the key to data modeling is stepping back and asking,
- 2:55Speaker 2: what is Tesla a type of Ah?
- 2:57Speaker 1: Okay, it's a company.
- 2:58Speaker 2: Exactly, it's a company. Therefore, your generalized entity is just company.
- 3:02Speaker 1: Okay, I think I get it. It's kind of like
- 3:04Speaker 1: those those labeled tepperware containers in your fridge.
- 3:07Speaker 2: Oh I like that analogy.
- 3:08Speaker 1: Yeah, Like you wouldn't take a sharpie and permanently right
- 3:11Speaker 1: last Tuesday spaghetti on the plastics, right, because if you
- 3:14Speaker 1: do that, the container is completely useless When you make
- 3:17Speaker 1: chicken next week.
- 3:18Speaker 2: Yeah, you'd have to throw it out or cross it.
- 3:19Speaker 1: Out, right. Instead, you label it pasta or just leftovers.
- 3:24Speaker 1: That generic label is your entity. That way your system
- 3:27Speaker 1: can handle whatever specific meal you decide to put inside
- 3:30Speaker 1: it tomorrow.
- 3:31Speaker 2: That is a perfect way to look at it. Future
- 3:34Speaker 2: proofing your software depends entirely on that kind of generalization.
- 3:37Speaker 2: By creating that generic company container, your system can scale infinitely.
- 3:42Speaker 2: You can track any publicly traded company without ever having
- 3:45Speaker 2: to rewrite the foundational rules of your app.
- 3:48Speaker 1: Makes total sense.
- 3:49Speaker 2: And just to keep things uniform, software architects follow strict
- 3:52Speaker 2: naming conventions for entities. We always use the singular form,
- 3:56Speaker 2: and we use camel case, So no spaces, right, no spaces,
- 4:00Speaker 2: and you capitalize the first letter of each word, so
- 4:02Speaker 2: capital C company.
- 4:04Speaker 1: Okay, so we've got our first container. The next question
- 4:06Speaker 1: we have to ask ourselves is what other kind of
- 4:08Speaker 1: information needs its own box? Like we want to track
- 4:12Speaker 1: daily stock values, right, so would our second entity just
- 4:15Speaker 1: be stock price? Capital S, capital P, no spaces.
- 4:19Speaker 2: You nailed it, But right now those are just empty boxes.
- 4:22Speaker 2: To make them actually useful, we have to characterize them.
- 4:25Speaker 2: We need to give them attributes.
- 4:26Speaker 1: Okay, attributes, So those are like the defining characteristics.
- 4:29Speaker 2: Of the entity exactly. So for our company entity, a
- 4:32Speaker 2: pretty essential attribute would be its name, and we'd probably
- 4:36Speaker 2: also want its ticker symbol.
- 4:37Speaker 1: Got it? And is there a naming rule for those two?
- 4:40Speaker 2: There is? For attributes, we use snake case, so it's
- 4:43Speaker 2: all lowercase, and we use underscores instead of spaces, so
- 4:46Speaker 2: ticker symbol is written ticker underscore symbol.
- 4:49Speaker 1: Okay, So snake case with the attributes inside the box
- 4:52Speaker 1: camel case for the box itself, you got it. And
- 4:55Speaker 1: for our stock price entity, the obvious attributes would be
- 4:58Speaker 1: the actual price. Maybe the data was recorded something like
- 5:01Speaker 1: capshured at.
- 5:02Speaker 2: Perfect Yes, But identifying the attributes is really just scratching
- 5:06Speaker 2: the surface here, because we can't just throw raw data
- 5:09Speaker 2: into these tupperware containers randomly.
- 5:11Speaker 1: We can't.
- 5:12Speaker 2: Now we have to declare exactly what kind of data
- 5:15Speaker 2: each attribute is allowed to hold.
- 5:17Speaker 1: Ah. Okay, this brings us back to that multimillion dollar
- 5:20Speaker 1: crash I mentioned at the very.
- 5:21Speaker 2: Beginning, doesn't it absolutely does. Look, databases aren't just digital
- 5:26Speaker 2: filing cabinets where you shove loose paper. They are incredibly
- 5:30Speaker 2: rigid math and slowing engines, right, right. So if you
- 5:33Speaker 2: tell a database to calculate the average stock price for
- 5:36Speaker 2: the year, and somewhere in those thousands of records, someone
- 5:39Speaker 2: typed the word three, like literally spelled out three instead
- 5:44Speaker 2: of the number three.
- 5:45Speaker 1: The math inj she just throws it hands up exactly.
- 5:48Speaker 2: Cannot compute it. It panics, and the whole thing crashes.
- 5:50Speaker 2: So to prevent that, we assign very strict data types
- 5:54Speaker 2: to every single attribute.
- 5:55Speaker 1: Okay, let's run through this menu of constraints then, and
- 5:58Speaker 1: I want to stick with our tupperware analogy, go for it.
- 6:00Speaker 1: So first we have the string data type. Now, this
- 6:03Speaker 1: is for alphanumeric characters, so letters, number, symbols, whatever. Yeah,
- 6:07Speaker 1: I imagine this is like your standard medium sized tupperware.
- 6:10Speaker 1: It's flexible and it's perfect for holding a company name
- 6:13Speaker 1: or a ticker symbol.
- 6:13Speaker 2: That's a great fit. Then you have the integer data type.
- 6:16Speaker 2: Now this is strictly for whole numbers positive or negative
- 6:20Speaker 2: doesn't matter, but absolutely no decimals.
- 6:22Speaker 1: Okay, So an integer is basically a container with molded
- 6:25Speaker 1: slots specifically for whole eggs, Like you physically cannot force
- 6:29Speaker 1: half an egg into it.
- 6:30Speaker 2: Yes, exactly.
- 6:31Speaker 1: Well, wait for our stock price entity. The price attribute
- 6:34Speaker 1: needs dollars and cents. I mean, an intager isn't going
- 6:38Speaker 1: to work for a stock price of three hundred dollars
- 6:40Speaker 1: and fifty cents.
- 6:41Speaker 2: It won't, which is why we use the decimal data
- 6:43Speaker 2: type instead. It is specifically engineered to handle numeric values
- 6:47Speaker 2: with high precision decimal places, and that is absolutely vital
- 6:51Speaker 2: for monetary values.
- 6:52Speaker 1: Oh, because you don't want the computer rounding your money
- 6:54Speaker 1: incorrectly precisely.
- 6:56Speaker 2: You might hear about other types like float or double
- 6:59Speaker 2: if you explore their languages, but decimal ensures the math
- 7:03Speaker 2: engine doesn't introduce weird rounding errors when it's calculating financial data.
- 7:07Speaker 1: Okay, good to know. Then we also have the boolean
- 7:10Speaker 1: data type. That one is strictly true or false.
- 7:12Speaker 2: Right, it's true or false.
- 7:13Speaker 1: Yeah. So in our fridge analogy, this is that ultra
- 7:16Speaker 1: thin container specifically designed to hold exactly one slice of
- 7:20Speaker 1: American cheese.
- 7:21Speaker 2: Wow. Yes, very specific, but yes, it's just highly optimized.
- 7:25Speaker 1: It takes up the bare minimum amount of memory because
- 7:27Speaker 1: it only ever answers it yes or no question like
- 7:29Speaker 1: is the market open? True or false?
- 7:31Speaker 2: Exactly. And we also use the text data type for
- 7:34Speaker 2: massive quantities of characters, like if you had a multipage
- 7:37Speaker 2: company history. And finally, the date data type.
- 7:40Speaker 1: Which would be perfect for our capture data attribute on.
- 7:43Speaker 2: The stock price exactly because the database actually knows how
- 7:47Speaker 2: to chronologically sort dates rather than just reading them as
- 7:50Speaker 2: a random string of numbers.
- 7:51Speaker 1: Okay, so we've got our data types. Yeah, and there
- 7:53Speaker 1: is one final crucial attribute that every entity just gets automatically, right,
- 7:59Speaker 1: the ID.
- 8:00Speaker 2: Yes, the ID is a unique integer. The second you
- 8:04Speaker 2: put a record into the database, it just automatically gets
- 8:07Speaker 2: stamped with this unique barcode number.
- 8:09Speaker 1: Okay, so stepping back, let's look at what we've built.
- 8:13Speaker 1: We have a box for companies and a box for
- 8:15Speaker 1: stock prices, and they both have really strict rules for
- 8:18Speaker 1: what goes inside them. They do, but right now they're
- 8:21Speaker 1: sitting on completely opposite sides of the room, totally ignoring
- 8:24Speaker 1: each other. Like, how does a floating price of three
- 8:27Speaker 1: hundred dollars know whose stock it actually belongs to?
- 8:30Speaker 2: And this is where we start weaving the invisible threads.
- 8:32Speaker 2: We have to establish relationships.
- 8:34Speaker 1: Okay, how do we do that?
- 8:35Speaker 2: In our scenario, we're defining what we call a one
- 8:38Speaker 2: to many relationship. Think about it. A single company like
- 8:41Speaker 2: Tesler has many stock prices over its lifespan, right, a
- 8:45Speaker 2: new one every day exactly. But conversely, one single stock
- 8:49Speaker 2: price record belongs to one specific company. So to make
- 8:53Speaker 2: that structural link, we use something called a foreign key.
- 8:55Speaker 1: A foreign key, okay, So we take that unique automatic
- 8:58Speaker 1: ID from the company record and we plant a reference
- 9:01Speaker 1: to it directly inside the stock price entity.
- 9:04Speaker 2: Yes, you got it.
- 9:05Speaker 1: Like, we literally create an attribute on the stock price
- 9:08Speaker 1: called companied. So if the database assigns Tesla an ID
- 9:12Speaker 1: of one, every single time we record a new daily
- 9:16Speaker 1: price for Tesla, we just stamp that price record with
- 9:19Speaker 1: a companied of one. Yep.
- 9:20Speaker 2: And if Facebook gets ID two, all its prices get
- 9:23Speaker 2: stamped with a two.
- 9:24Speaker 1: But hold on, you're losing me a bit here. Why
- 9:26Speaker 1: are we playing matching games with numbers?
- 9:28Speaker 2: What do you mean?
- 9:29Speaker 1: Well, if you're listening to this, you're probably wondering, why
- 9:31Speaker 1: not just add an attribute called company name directly to
- 9:34Speaker 1: the stock price and just take the word Tesla every time.
- 9:37Speaker 1: I mean, it feels way less complicated to just read
- 9:40Speaker 1: the name instead of cross referencing numbers.
- 9:42Speaker 2: I get that, and Honestly, it is the most common
- 9:44Speaker 2: temptation for beginners, but it inevitably leads to disaster. Really why,
- 9:49Speaker 2: because a concept called data normalization. Let's imagine you take
- 9:52Speaker 2: the easy route. You hardcode the string Tesla into say,
- 9:55Speaker 2: ten thousand daily stock records. Okay, seems fine so far,
- 9:59Speaker 2: but then tomorrow Tesla decides to rebrand. They change their
- 10:03Speaker 2: corporate name entirely.
- 10:04Speaker 1: Oh wow. Yeah, if I hard coded the name, I
- 10:07Speaker 1: suddenly have a massive problem.
- 10:08Speaker 2: A huge problem. You would have to search through ten
- 10:11Speaker 2: thousand individual stock records and update that exact string of
- 10:15Speaker 2: text manually, and if you miss even one.
- 10:18Speaker 1: My financial reports are completely fractured.
- 10:20Speaker 2: Exactly. But by linking everything to a single company ID instead,
- 10:24Speaker 2: the name Tesla only actually exists in one single place
- 10:29Speaker 2: in your entire.
- 10:30Speaker 1: Architecture, inside the company record with ID one.
- 10:32Speaker 2: Yes, those ten thousand stock prices, they don't care what
- 10:35Speaker 2: the company is called. They just point to ID one.
- 10:37Speaker 2: So if the company changes its name, you just updated
- 10:40Speaker 2: exactly once in that root container.
- 10:42Speaker 1: Well that's brilliant. So it's basically a cascading update. I
- 10:44Speaker 1: changed it at the root level, and all ten thousand
- 10:48Speaker 1: branches instantly reflect the new reality because the mathematical relationship
- 10:52Speaker 1: is what remains intact exactly.
- 10:54Speaker 2: It saves hours of work, it completely eliminates human error,
- 10:58Speaker 2: and it creates what we call a single source of truth.
- 11:01Speaker 1: A single source of truth. I love that. Okay, so
- 11:03Speaker 1: we have our theory locked in, we have our blueprints,
- 11:06Speaker 1: but you know, blueprints don't keep the rain out. We
- 11:08Speaker 1: actually need to transition from these concepts into the would
- 11:11Speaker 1: be on RAILS framework. We need to build a house.
- 11:14Speaker 2: Let's build it now.
- 11:15Speaker 1: If you're listening to this and thinking, oh, I don't
- 11:17Speaker 1: know how to code, this is where I get lost,
- 11:20Speaker 1: take a breath. The code itself isn't the main focus here.
- 11:23Speaker 1: The point is actually understanding how much heavy lifting this framework.
- 11:26Speaker 2: Does for you, because back in the day, translating these
- 11:29Speaker 2: blueprints meant writing hundreds of lines.
- 11:32Speaker 1: Of raw sequel SEQL structured query language right.
- 11:35Speaker 2: SEQL is the native tongue of databases, and it is
- 11:39Speaker 2: notoriously unforgiving. I mean, you miss one semicolon and nothing works.
- 11:43Speaker 1: The RAILS changes the game, right because it uses something
- 11:46Speaker 1: called an OARM.
- 11:47Speaker 2: Yes, object relational mapping, and the specific library that RAILS
- 11:51Speaker 2: uses for this is called active record.
- 11:53Speaker 1: So how does that work.
- 11:54Speaker 2: You can basically think of an OARM as a hyper efficient,
- 11:57Speaker 2: real time foreign language translator. On one side, you are
- 12:02Speaker 2: thinking and speaking in Ruby, which is a very intuitive
- 12:05Speaker 2: object oriented programming language. But on the other side is
- 12:09Speaker 2: the database and it's sitting there expecting these rigid SQL commands.
- 12:13Speaker 2: Active record just sits in the middle. It translates your
- 12:16Speaker 2: simple Ruby concepts perfectly into complex seqls, so you never
- 12:19Speaker 2: actually have to write it yourself.
- 12:21Speaker 1: That is amazing. So let's see that translator in action.
- 12:23Speaker 1: Let's start with our independent entity, the company. In the
- 12:27Speaker 1: Rails command line. You just type a really simple instruction
- 12:31Speaker 1: Rails generate model company name dot string tick or symbol
- 12:36Speaker 1: dot string, and that's it.
- 12:38Speaker 2: That's all it takes. And notice how we didn't mention
- 12:41Speaker 2: the ID in that command at all. Rails automatically handles
- 12:44Speaker 2: creating that unique barcode for us.
- 12:47Speaker 1: So when you hit enter, what happens.
- 12:49Speaker 2: Well, the framework acts like a massive engine. It generates
- 12:52Speaker 2: a bunch of files for you automatically. But two of
- 12:54Speaker 2: them are super critical for our data model, migration file
- 12:57Speaker 2: and the bottle file.
- 12:58Speaker 1: Okay, so the migration file first, that's a Ruby script.
- 13:00Speaker 2: Right, It contains the literal instructions to physically alter the
- 13:04Speaker 2: database architecture. It tells the system, hey, carve out a
- 13:08Speaker 2: new table called companies, and you'll notice it automatically pluralized
- 13:12Speaker 2: the name for the table.
- 13:12Speaker 1: Because it's holding many companies. Makes sense. And then the
- 13:15Speaker 1: model file is a Ruby class file that represents the
- 13:19Speaker 1: singular concept company exactly.
- 13:21Speaker 2: This is the object side of our translator. But it's
- 13:24Speaker 2: important to note just generating these files doesn't actually do
- 13:27Speaker 2: anything to the database yet.
- 13:29Speaker 1: Right, we've basically just drawn up the work order exactly.
- 13:32Speaker 2: To execute those work orders, you have to run another
- 13:34Speaker 2: command rails dB dot migrate.
- 13:38Speaker 1: Oh.
- 13:38Speaker 2: That command looks at your migration script, translates it into
- 13:42Speaker 2: a gnarly SQL create table command and physically builds the structure.
- 13:46Speaker 2: It also updates a master document called schema dot rb.
- 13:50Speaker 1: Okay, I have to stop you there, because whenever I
- 13:53Speaker 1: hear developers talk about the schema file, it's always with
- 13:56Speaker 1: this mix of like reverence and absolutely fear.
- 14:00Speaker 2: Oh for good reason.
- 14:01Speaker 1: It's essentially the ultimate map of your entire database. It
- 14:04Speaker 1: shows every table, every column that actually exists, but there
- 14:07Speaker 1: is a massive warning label on it. Never open it
- 14:10Speaker 1: and type in it directly.
- 14:11Speaker 2: Yeah, modifying schema dot RB by hand is a recipe
- 14:15Speaker 2: for catastrophic desynchronization.
- 14:17Speaker 1: Catastrophic desynchronization. Yeah, that sounds bad.
- 14:20Speaker 2: It is because if you bypass the migration files and
- 14:23Speaker 2: just type directly into the schema, your Rail's application thinks
- 14:27Speaker 2: the database looks one way, but the actual database engine
- 14:30Speaker 2: behaves a completely different way. It's a mess. Always always
- 14:34Speaker 2: use the Rails dB dot migrate command to orchestrate changes securely.
- 14:39Speaker 1: Step away from the schema. Got it. Okay, okay, so
- 14:42Speaker 1: the company's table is officially built. Now we have to
- 14:45Speaker 1: tackle the stock price table. So we type rails generate
- 14:48Speaker 1: model stockprice price dot decimal, capture date dot date perfect. Wait,
- 14:54Speaker 1: how do we handle that invisible thread we talked about?
- 14:56Speaker 1: Do we just type company dot integer to manually create
- 14:59Speaker 1: that foreign key?
- 15:00Speaker 2: I mean, you certainly could do it that way, but
- 15:02Speaker 2: Rails actually offers a really brilliant shortcut that just proves
- 15:06Speaker 2: the power of the framework. Instead of manually declaring an
- 15:09Speaker 2: integer field, you simply type company dot references.
- 15:12Speaker 1: Just the word references. That does all the heavy lifting.
- 15:14Speaker 2: It does. It acts as its own special data type.
- 15:16Speaker 2: In this context, when the generator sees the word references,
- 15:19Speaker 2: it dynamically looks for a model named company. It then
- 15:22Speaker 2: automatically creates the companied integer column for you, and crucially,
- 15:26Speaker 2: it writes all the complex sequel constraints into the database
- 15:30Speaker 2: to ensure that the foreign key is secure and properly
- 15:33Speaker 2: indexed for fast searching. You just run Rails dB dot
- 15:37Speaker 2: migrate again and your entire relational architecture is brought to life.
- 15:41Speaker 1: That is so cool. Okay, so we have the house,
- 15:44Speaker 1: the tables exist, the strict rules are applied, and the
- 15:47Speaker 1: relationships are mathematically sound. But you know, a database without
- 15:51Speaker 1: data is really just an expensive, empty filing cabinet.
- 15:54Speaker 2: Very true.
- 15:55Speaker 1: We need to step into the control room and prove
- 15:57Speaker 1: that this architecture actually works.
- 15:58Speaker 2: And this is exactly where the Rails console comes in.
- 16:01Speaker 1: The Rails console.
- 16:02Speaker 2: Yeah. By just typing Rails console in your terminal, you
- 16:05Speaker 2: drop right into a sandbox environment. It lets you interact
- 16:08Speaker 2: with your database using pure Ruby. You can test out
- 16:10Speaker 2: all your models and relationships before you ever even worry
- 16:13Speaker 2: about building a user interface or web pages.
- 16:16Speaker 1: Okay, so we're in the sandbox. Let's create our very
- 16:18Speaker 1: first piece of data. I type a variable let's call
- 16:21Speaker 1: it C and I set an equal to company dot new.
- 16:25Speaker 1: I give it the name Tesla and the ticker symbol TSLA.
- 16:29Speaker 2: Okay, But at this exact moment, is it in the database.
- 16:31Speaker 1: No, right, it's just sort of hovering in the applications
- 16:35Speaker 1: temporary memory.
- 16:36Speaker 2: Exactly. It exists only in the memory of your current session.
- 16:39Speaker 2: If you close the console right then it just vanishes.
- 16:42Speaker 2: To persist that data, To permanently write it into the
- 16:45Speaker 2: hard drive of the database, you have to call an
- 16:47Speaker 2: instance method on that object. You type C dot.
- 16:51Speaker 1: Save, And this is where the magic really clicks for me.
- 16:53Speaker 1: You hit enter on dot save and you don't touch
- 16:56Speaker 1: a spreadsheet. You don't have to write some crazy SEQL
- 16:59Speaker 1: insert statement. Yep. The active record or m grabs that
- 17:02Speaker 1: hovering object, translates it into database language securely, ejects it
- 17:06Speaker 1: into the company's table, and instantly kicks back at assigned
- 17:09Speaker 1: idea of one. It just wove your concept into a
- 17:12Speaker 1: permanent reality.
- 17:13Speaker 2: It really is satisfying, and the power of that abstraction
- 17:16Speaker 2: becomes even more obvious when we start dealing with our relationships.
- 17:20Speaker 2: Let's create a stock price. Now, we start by initializing
- 17:23Speaker 2: a new one pg'al khiel stockprice Dot new and we
- 17:25Speaker 2: give it a price of say five hundred dollars in
- 17:27Speaker 2: a date. Okay, now we need to link it to Tesla.
- 17:30Speaker 2: And remember, we don't want to play matching games with
- 17:33Speaker 2: integer ideas if we don't have to.
- 17:35Speaker 1: Right, Because in our actual RAILS model files we use
- 17:38Speaker 1: those keywords belongs to Dot company and has many Dot
- 17:43Speaker 1: stock prices, we explicitly told the translator how they relate.
- 17:47Speaker 2: To each other exactly. And because you declared that relationship,
- 17:50Speaker 2: RAILS allows you to simply pass the actual company object.
- 17:53Speaker 2: That variable C we just saved directly to the stock price.
- 17:56Speaker 1: You don't pass the number one, you pass the whole variable.
- 17:58Speaker 2: We passed the whole object. The OORM looks at it, intelligently,
- 18:02Speaker 2: extracts the primary key idea of one out of it,
- 18:05Speaker 2: and slots it perfectly into the companied foreign key column.
- 18:08Speaker 2: Then you hit P save and the invisible thread is
- 18:11Speaker 2: permanently secured in the database.
- 18:13Speaker 1: That is wild. It is abstracting away all that tedious
- 18:16Speaker 1: wiring and getting that data back out is just as intuitive, rightfinitely, Like,
- 18:20Speaker 1: if I want to see a history of Tesla stock prices,
- 18:22Speaker 1: I don't write a massive search query. I just take
- 18:24Speaker 1: my company variable C and I type c dot stock prices,
- 18:27Speaker 1: and the ORM just instantly fetches an array containing every
- 18:31Speaker 1: single related stock price object.
- 18:33Speaker 2: It's basically magic, and you can manipulate existing data with
- 18:36Speaker 2: the exact same ease. Say Tesla decides to shorten its
- 18:40Speaker 2: ticker symbol to just TLA, you don't have to create
- 18:43Speaker 2: a new record.
- 18:44Speaker 1: What do you do?
- 18:44Speaker 2: You just grab the existing object and use the dot
- 18:47Speaker 2: update method and you pass in the new string. The
- 18:51Speaker 2: ORM builds in SQL update query behind the scenes and
- 18:54Speaker 2: just executes it instantly.
- 18:56Speaker 1: What if a company goes completely bankrupt and we want
- 18:58Speaker 1: to like purge it for our system entirely, do we
- 19:01Speaker 1: have to manually delete every single daily stock price first
- 19:04Speaker 1: so it doesn't crash?
- 19:06Speaker 2: Not at all. You have very powerful destructive methods at
- 19:09Speaker 2: your disposal. You can call dot destroy on a specific record,
- 19:13Speaker 2: and depending on how you configured your hasmini relationship earlier,
- 19:16Speaker 2: you can actually tell rails that if a company is destroyed,
- 19:19Speaker 2: it should automatically cascade down and destroy all associated stock prices.
- 19:23Speaker 1: Oh, so you don't end up with a bunch of
- 19:25Speaker 1: orson data floating in the voice exactly.
- 19:27Speaker 2: It cleans up after itself.
- 19:28Speaker 1: So with just a few intuitive English like commands like
- 19:32Speaker 1: dot save, dot update, dot destroy, we are commanding a massive,
- 19:36Speaker 1: complex mathematical engine securely.
- 19:38Speaker 2: Yeah, the sheer complexity of SEQL, the rigidness of the
- 19:42Speaker 2: database tables, it's all abstracted away. The framework allows you
- 19:46Speaker 2: to focus purely on the logic of your real world
- 19:48Speaker 2: problem instead of fighting the syntax.
- 19:50Speaker 1: Okay, left, take a breath and look at the terrain
- 19:52Speaker 1: we've covered today. We started with the chaotic idea of
- 19:55Speaker 1: tracking fluctuating stocks. We learned that the first defense against
- 19:59Speaker 1: catastrophic failure is generalizing those specific concepts into entities like
- 20:05Speaker 1: company and stock price. Right, we gave them attributes, and
- 20:08Speaker 1: we constrain them with strict data types, treating the database
- 20:12Speaker 1: as a math engine rather than just a filing cabinet.
- 20:14Speaker 1: We wired them together using the one to many relationship
- 20:17Speaker 1: and foreign keys to create a single source.
- 20:20Speaker 2: Of truth, which is so important.
- 20:22Speaker 1: And finally, we use the RAILS framework and its active
- 20:24Speaker 1: record OARM to effortlessly translate those blueprints into a fully functioning,
- 20:30Speaker 1: queriable data ecosystem.
- 20:31Speaker 2: You have officially moved from abstract thought to designing concrete
- 20:35Speaker 2: information architecture.
- 20:37Speaker 1: Absolutely so to everyone listening right now, we want to
- 20:40Speaker 1: leave you with a quick thought exercise, just to test
- 20:42Speaker 1: your new software architect mindset.
- 20:44Speaker 2: I love a good thought exercise.
- 20:46Speaker 1: Let's say you decide your financial tracker needs to include
- 20:49Speaker 1: the CEO of each company based on the rules of
- 20:52Speaker 1: generalization and data normalization we discussed today, how would you
- 20:55Speaker 1: build it? Good question? Should CEO just be a simple
- 21:00Speaker 1: string attribute added directly to the existing company container, or
- 21:04Speaker 1: should CEO be its own, completely separate entity that gets
- 21:07Speaker 1: linked via a foreign key? Right, ask yourself what happens
- 21:12Speaker 1: to your data if a CEO steps down and a
- 21:14Speaker 1: new one takes over, and you want to preserve the
- 21:17Speaker 1: history of who was exactly in charge when a specific stockspiked.
- 21:21Speaker 2: That scenario is the ultimate test of finding the right
- 21:24Speaker 2: level of normalization for your app. And you know, as
- 21:27Speaker 2: you go about your day, I'd really encourage you to
- 21:29Speaker 2: start looking at the digital world differently. What do you
- 21:32Speaker 2: mean next time you open a shopping app or you're
- 21:34Speaker 2: just scrolling through social media, try to look past the
- 21:36Speaker 2: flashy photos and the polished user interface. Try to actually
- 21:40Speaker 2: spot the invisible architecture. Look for the entities try to
- 21:44Speaker 2: guess what the attributes are, well, it's fine, yeah. Imagine
- 21:48Speaker 2: the hidden foreign keys that are secretly binding that entire
- 21:50Speaker 2: digital universe together.
- 21:52Speaker 1: Behind the screen, it is all just carefully defined containers
- 21:55Speaker 1: in visible threads. Suddenly, that infinitely comeplay software doesn't look
- 22:00Speaker 1: like magic anymore. It's just an architecture waiting for you
- 22:02Speaker 1: to define the rules.