00:00:00.560
um I actually originally set out to
00:00:01.880
create a talk about how databased
00:00:03.399
migrations work behind the scenes but
00:00:04.920
ended up realizing that I have a lot to
00:00:07.160
learn about active record first and
00:00:08.480
created this talk instead so this is
00:00:10.920
from confusion to Clarity demystifying
00:00:13.160
active record in
00:00:14.759
rails this is Zoe she's learning to code
00:00:18.080
and attended a recent rails girls event
00:00:20.640
where she went from knowing nothing
00:00:21.920
about rails to building a web
00:00:23.320
application using Ruby on Rails all in
00:00:25.640
one day with the help of a
00:00:27.920
mentor rails girls has a really handy
00:00:30.560
guide that Zoe followed and the code
00:00:32.520
that we'll see today is the result of
00:00:34.120
her time at the event rails Zoe learned
00:00:37.079
is a web application framework written
00:00:38.920
in Ruby that follows the model view
00:00:40.960
controller and thec architecture it
00:00:43.719
streamlines web development by providing
00:00:45.680
conventional conventions tools and a
00:00:47.800
vibrant ecosystem of gems enabling
00:00:49.680
developers to quickly build and maintain
00:00:51.640
web applications with minimal
00:00:53.640
configuration Zoe used rails. new to
00:00:56.239
create the app and rails generate
00:00:58.120
scaffold to create her a model
00:01:00.480
View and controller for her ideas she
00:01:03.039
was very impressed by how little code
00:01:04.680
she actually needed to write with Ra's
00:01:06.360
magic doing so much for her by the end
00:01:08.880
of the day she was really pleased with
00:01:10.360
the website she created to keep track of
00:01:12.240
all her ideas for her dog
00:01:15.040
Cody Zoe learned that the data for her
00:01:17.640
ideas was being stored very similarly to
00:01:20.000
a spreadsheet using columns and rows to
00:01:22.280
arrange the data and that this was
00:01:24.119
called a relational database relational
00:01:26.520
databases as it turns out are one of the
00:01:28.799
most popular types of dat databases they
00:01:30.920
store and provide access to data points
00:01:32.920
that are related to one
00:01:34.360
another but Zoe wanted to learn more
00:01:36.560
about how it all worked how an idea
00:01:38.600
named make Cody happy gets from the
00:01:40.520
database to her shiny new website so she
00:01:43.799
continued her learning and discovered
00:01:45.320
that her database fresh shof rails. new
00:01:47.840
was a SQL light database and something
00:01:50.000
magical called active record was
00:01:51.799
enabling her to interact with the
00:01:53.399
database in her favorite language Ruby
00:01:56.240
active record she learned from a quick
00:01:57.880
Google search comes out of the box with
00:02:00.479
so it's been there all along working in
00:02:02.520
the background since she started working
00:02:03.960
on her web app
00:02:06.159
neat to talk about active record she
00:02:08.560
learned is to talk about object
00:02:10.399
relational mapping or o RM for short so
00:02:14.879
active record is basically she
00:02:16.760
understood a translator Zoe gets to
00:02:19.720
write her Ruby code to get information
00:02:21.560
from the database and active record does
00:02:23.959
what it needs to do to retrieve that
00:02:25.680
information from the database and send
00:02:27.680
it back to her in a format that she can
00:02:29.319
use that's a good start but zoee wasn't
00:02:31.959
satisfied that that was the full extent
00:02:33.680
of what active record could do and she
00:02:35.840
wasn't satisfied with the outof Box
00:02:37.640
explanation for rails
00:02:39.640
either but she was satisfied with her
00:02:41.959
experience in the console so far if she
00:02:44.200
wanted to see all the ideas she had in
00:02:45.920
her D database she could use idea. all
00:02:49.080
and they were displayed in a really nice
00:02:50.640
userfriendly way since she used a
00:02:53.040
variable toore all her ideas she can
00:02:55.440
find out what its class is which is an
00:02:57.360
active record relation so already active
00:02:59.920
record is hard at work and she can do a
00:03:02.599
whole lot more like count them and add
00:03:05.319
conditional statements you using wear
00:03:08.000
Zoe sees all the SQL in her output as
00:03:10.440
well the dark blue what her database is
00:03:13.080
responding with to her Ruby code
00:03:14.840
certainly writing Ruby is much simpler
00:03:16.959
she
00:03:18.200
reflects feeling more grateful than ever
00:03:20.680
about otive record Zoe sets out to find
00:03:22.920
out how exactly active record is set up
00:03:25.720
out of the box with rails she identifies
00:03:28.480
four main areas in her app to explore
00:03:31.280
gem file default configuration database
00:03:34.239
configuration and model
00:03:36.840
generation so he starts with the gem
00:03:39.000
file feeling like it looks the most
00:03:41.120
straightforward from the list but before
00:03:43.360
heading to the gem file she wants to
00:03:45.159
confirm what exactly a ruby gem is a
00:03:48.200
ruby gem is code that has been written
00:03:50.159
for a specific purpose that she can use
00:03:52.280
in her work so she doesn't have to write
00:03:54.000
it all herself running rails. new to
00:03:56.599
create her rails app has included a lot
00:03:58.519
of gems by default
00:04:00.760
they're often referred to as a library
00:04:02.959
or package and Zoe learns that anyone
00:04:05.319
can create a g and silently commits
00:04:07.480
herself to creating one in the future
00:04:10.200
rubygems.org is a site where you can
00:04:12.360
find install and publish Ruby jems just
00:04:15.319
like it says on the website satisfied
00:04:17.959
she goes on to her gem file in the rails
00:04:19.799
girls
00:04:21.120
application the first thing Zoe notices
00:04:23.360
in her gem file is the source online one
00:04:26.280
this indicates that all the gems should
00:04:27.840
be retrieved from rubygems.org ORS the
00:04:30.560
site she was just on if she needs a gem
00:04:33.160
that isn't on this site she could
00:04:34.840
specify the site to retrieve it from for
00:04:36.759
that specific gem she finds the gem
00:04:39.520
rails and sees that it adheres to centic
00:04:41.960
versioning where each version number
00:04:43.840
consists of three parts major seven
00:04:46.880
minor zero and Patch
00:04:49.520
four Zoe Ventures back to rubygems.org
00:04:52.479
site to see what her specific rails gem
00:04:55.039
looks like she can see that there are a
00:04:57.160
lot of dependencies which are gems
00:04:59.080
themselves
00:05:00.199
when she uses bundler to install her
00:05:02.000
gems she can expect an active record gem
00:05:04.560
version
00:05:05.479
7.0.4 to be installed which she would be
00:05:08.199
able to see in Gem file. loock Gem file.
00:05:11.440
loock keeps track of all the specific
00:05:13.280
versions of the gems she to use in her
00:05:17.000
application Jem file delock is quite
00:05:19.520
overwhelming but she does a quick search
00:05:21.160
in the file and finds all the places
00:05:22.919
where active record exists surprisingly
00:05:25.639
the version written here is 7.0.0
00:05:29.840
not
00:05:31.280
7.0.4 Zoe heads back to the gem file to
00:05:34.080
confirm if she's remembering correctly
00:05:36.360
yep
00:05:37.600
7.0.4 but what she missed before is the
00:05:39.919
tier and greater than side so this means
00:05:43.240
that her app can use any version of
00:05:44.919
rails that is greater than or equal to
00:05:48.360
7.0.4 without going to the next minor
00:05:50.759
release the second number so
00:05:54.120
7.0.3 would never appear in her gem
00:05:56.840
file. loock file 7.0 .8 could which is
00:06:00.680
what it currently is and 7.1 would never
00:06:04.120
appear as it's the next minor release
00:06:06.560
yep that explains why the versions are
00:06:08.199
different between jum file and jum file.
00:06:11.120
loock another quick look at jum file.
00:06:13.560
loock and Zoe understands its importance
00:06:16.199
at the time of creating her rails girls
00:06:18.400
app rails gem
00:06:20.160
7.0.0 was used and this version was
00:06:22.720
saved in gem file. loock this means that
00:06:25.479
anyone who wants to collaborate with her
00:06:27.400
and ren bundle will be working up the
00:06:29.360
same rails version even if
00:06:31.680
7.1.0 is available and her collaborator
00:06:34.520
is using it on another app neat feeling
00:06:37.840
satisfied with her gem file learning Zoe
00:06:39.599
heads back to her
00:06:41.319
list next up is default configuration
00:06:44.319
and for this Zoe dives into
00:06:48.800
application.rb Zoe hes in on line three
00:06:51.599
require rails all rails she learned is a
00:06:55.120
modular framework composed of various
00:06:57.080
components and each is responsible for
00:06:58.919
different aspects of web development
00:07:01.240
these components include active record
00:07:03.160
for database interaction which is all
00:07:05.240
Zoe is really interested for today
00:07:07.800
action pack for handling web requests
00:07:10.479
action mailer for handling for email
00:07:12.680
handling active support for utility
00:07:14.960
functions and extensions and also more
00:07:17.720
zo I learns that you could in fact omit
00:07:19.639
this line of code and require each of
00:07:21.560
the components individually if you
00:07:23.319
didn't need all of them but going down
00:07:25.080
that rabbit hole is solidly out of scope
00:07:26.840
for today so he also notices though that
00:07:29.520
but there is no all file in her rails
00:07:32.800
application turns out you have to go
00:07:34.800
right to the source the rail source code
00:07:36.520
that is to find all.
00:07:38.599
RB Zoe sees active record on line 7 and
00:07:42.000
decides she's satisfied here for now but
00:07:44.199
makes a note in her learning to do this
00:07:45.720
to come back and explore
00:07:48.120
later so this line requires the active
00:07:51.319
record component good to know the next
00:07:54.560
line indicates that Zoe can in the gem
00:07:56.840
file specify different sets of gems for
00:07:59.520
various environment groups so he
00:08:01.599
realizes she never even scrolled down
00:08:03.280
through her gem file and heads back to
00:08:05.360
investigate she Scrolls to the bottom
00:08:07.520
and sees that there are in fact three
00:08:09.199
groups a group for development and test
00:08:12.560
a group for development and a group for
00:08:14.520
test these are keyword that rails has
00:08:17.000
chosen but she could change these or add
00:08:19.599
different ones and group them however
00:08:21.360
she wants for example she could create a
00:08:23.840
group for production and development if
00:08:26.039
she had a gem that didn't need to be
00:08:27.560
used in her test environment
00:08:30.720
okay so this line is a dynamic way to
00:08:32.839
load the gem specified in the
00:08:34.680
application's gem file taking into
00:08:37.000
account the different groups defined for
00:08:38.519
various environments development test
00:08:40.760
and production in Zoe's case using
00:08:43.680
groups in the gem file ensures that only
00:08:45.640
the gems relevant to the current railes
00:08:47.240
environment are loaded which reduces
00:08:49.200
unnecessary dependencies and improves
00:08:51.160
performance this makes sense to zo but
00:08:53.360
has absolutely nothing to do with rails
00:08:55.320
or active record rather so she carries
00:08:57.279
on determined not to let herself get too
00:08:59.040
side
00:09:00.279
again next Zoe sees that she's defining
00:09:03.200
her application class within her rails
00:09:05.360
goals module and it's inheriting from
00:09:07.600
another class rail's
00:09:09.680
application in object or oriented
00:09:12.320
programming inheritance allows a class
00:09:14.200
to inherit behaviors and attributes from
00:09:16.320
another class a super class by
00:09:18.600
inheriting from rail's application Zoe's
00:09:21.160
application gains access to all the
00:09:22.959
functionality provided by the rails
00:09:24.680
framework for managing the application's
00:09:26.480
configuration and behavior so is already
00:09:29.519
venture to the rail source code so she
00:09:31.160
adds this to her list of things to
00:09:32.720
explore later
00:09:33.920
on there's a space in this class to load
00:09:36.440
specific configurations but that doesn't
00:09:38.600
have to do with active backround so she
00:09:40.000
adds load defaults to her learning list
00:09:41.640
as
00:09:42.600
well surprise she's already halfway
00:09:45.000
through Zoe forges on to the next item
00:09:47.480
database configuration and heads to
00:09:49.720
database.
00:09:51.279
Yano this file Zoe learns is essential
00:09:54.040
for specifying how active record should
00:09:56.040
connect to her
00:09:57.480
database similar to the group in her gem
00:09:59.880
file Zoe sees that this file is also
00:10:02.279
organized into sections for different
00:10:03.920
environments development test and
00:10:05.920
production but these keywords could be
00:10:08.200
changed Zoe sees that each environment
00:10:10.839
section contains the configuration
00:10:12.440
settings specific to the
00:10:14.360
environment there's also a default
00:10:16.399
section and Zoe sees that all her
00:10:18.440
environments are inheriting their
00:10:19.839
configuration from the default the only
00:10:22.240
unique attribute is the database name
00:10:25.399
she learns that line 8 is specifying the
00:10:27.440
database adapter that active record
00:10:29.320
should use to connect to the database
00:10:31.880
Zoe's out of the box rail setting is
00:10:33.800
using SQL light but she could come back
00:10:35.880
here and change this if she wanted use a
00:10:37.600
different one for example to use
00:10:39.560
postgressql she would need the PG gem
00:10:41.600
installed or if she wanted to use MySQL
00:10:43.920
she would need the MySQL gem installed
00:10:46.320
Zoe notices a comment above that
00:10:48.079
indicates she'll need the SQL light gem
00:10:49.880
to be installed to be able to use the
00:10:51.440
sqlite adapter for the database a quick
00:10:54.519
look back in the gem file confirms that
00:10:56.240
the SQL light gem is included which
00:10:58.120
makes sense because our app and this gem
00:11:00.240
is included by default when you run
00:11:02.279
rails.
00:11:03.760
new Zoe adds pool and time out to her
00:11:06.519
explore later list which is growing and
00:11:08.519
heads to the database keyword on line 14
00:11:11.600
in the case of sqlite the database is
00:11:13.560
stored as a file locally on the file
00:11:15.839
system the database keyword specifies
00:11:18.560
the path to the sqlite database file a
00:11:21.760
look to the left and Zoe does indeed see
00:11:23.760
the mentioned file in the database
00:11:25.240
folder neat unfortunately it's not in
00:11:28.920
the right format apparently but that
00:11:31.079
doesn't stop her from trying to see
00:11:32.560
what's in it
00:11:33.639
anyways not as satisfying as she was
00:11:35.839
hoping for but Zoe can see some words
00:11:37.839
that she knows are in her idea table so
00:11:39.880
that's
00:11:40.880
something she decides that she's too
00:11:43.000
curious not to try to see what's in her
00:11:44.720
database file and figures out how to
00:11:46.639
hack in at least that's what she
00:11:48.279
imagines herself doing so Zoe armed with
00:11:51.560
commands fresh from Google and chat gvt
00:11:53.760
changes directory into her DB database
00:11:56.399
folder and starts a SQL like console to
00:11:59.040
dive into development.
00:12:01.000
SQL once in she wants to see what's in
00:12:03.639
there so she writes a command to print
00:12:05.079
all the
00:12:05.920
tables interesting ideas make sense
00:12:09.680
schema migrations she's seen these words
00:12:11.839
before but AR internal metadata no idea
00:12:16.000
resolving to stay on track is she writes
00:12:18.440
a command and SQL to print all the rows
00:12:20.600
in her ideas
00:12:22.519
table she reflects that if she was in
00:12:24.680
the rails console active record would
00:12:26.480
enable her to write idea. all way Nic
00:12:30.800
yep those are the names of her ideas but
00:12:32.880
the formatting isn't great satisfied Zoe
00:12:36.040
moves on to investigate what's in this
00:12:37.639
schema migrations
00:12:39.240
file not as exciting as she was
00:12:41.440
expecting but Zoe does recall seeing
00:12:43.199
this number before she takes a look at
00:12:45.880
the Active record migration file that
00:12:47.600
was created when she ran the rails
00:12:49.120
generate scaffold command and sees that
00:12:51.399
this number is at the beginning of the
00:12:52.720
file name on closer inspection Zoe
00:12:55.199
realizes that this is actually a Tim
00:12:56.800
stamp for when the file was created year
00:12:59.639
2023 November 20th at 12:06 and 22nds
00:13:05.279
cool since there's only one table left
00:13:07.680
Zoe decides to take a look in AR
00:13:09.639
internal
00:13:11.160
metadata she writes the command to print
00:13:13.399
its contents and is pretty underwhelmed
00:13:15.720
about the results she adds AR internal
00:13:17.959
metadata onto her list of things to
00:13:19.639
learn and Retreats back to database.
00:13:22.480
Yo back where she started Zoe realizes
00:13:25.240
that there's actually nothing specific
00:13:26.760
to active record in this file but she's
00:13:29.360
still satisfied that she learned that
00:13:31.040
the app needs an adapter so the database
00:13:33.000
can talk to active
00:13:34.920
record last but not least model
00:13:37.160
generation with a trip to application
00:13:39.160
record.
00:13:40.639
RV let's review what Zoe did at the
00:13:42.880
rails G event to create her model she
00:13:45.880
read rails generate scaffold with the
00:13:48.279
table name then the attribute names and
00:13:50.360
their types this rail command created a
00:13:52.920
lot of files for her including the model
00:13:55.680
database migration file and some test
00:13:58.120
files and also created some other files
00:14:00.600
but Zoe's already taken a lot of detours
00:14:02.759
on her mission to understand active
00:14:04.199
record so she's not as concerned about
00:14:06.199
them right
00:14:07.480
now she heads back into the code and
00:14:09.880
starts with her idea class she did add a
00:14:12.279
couple of validations to this class she
00:14:14.399
wanted all her ideas to have a name and
00:14:16.399
didn't want to accidentally repeat
00:14:17.759
herself so she used active records
00:14:19.880
presence and uniqueness validation on
00:14:21.839
the name
00:14:23.120
attribute looking at her idea class she
00:14:25.759
sees that it's inheriting from
00:14:27.320
application record I look to the left
00:14:29.920
and Zoe sees that that's the only other
00:14:31.759
file in the models
00:14:33.279
folder application record Zoe learns is
00:14:35.880
an abstract class meaning that it will
00:14:37.839
never be called directly only inherited
00:14:40.000
by other class for example Zoe would not
00:14:42.759
be able to do application. all
00:14:45.360
application record. all in the console
00:14:47.680
in the same way she did idea.
00:14:50.120
all application record is the base class
00:14:52.720
for all the models in her app and it's
00:14:55.120
inheriting from active record basee this
00:14:57.839
inheritance is what turn turns what
00:14:59.440
would be would be a regular Ruby class
00:15:01.920
into an active record bottle in rails
00:15:04.839
active record base is the primary class
00:15:07.519
that provides the core functionality for
00:15:09.199
interacting with a relational database
00:15:11.480
magic happens
00:15:12.920
here but where is active record base
00:15:15.759
it's in the real source
00:15:17.959
code zo heads back to the real source
00:15:20.440
code and finds base. RB it's a pretty
00:15:23.720
big file at 338 lines at the time of the
00:15:26.720
screenshot but when she Scrolls down she
00:15:29.079
sees that it's basically a list of many
00:15:30.920
different files that are included or
00:15:32.839
extended
00:15:34.759
interesting feeling a bit overwhelmed
00:15:36.839
with the large list Zoe Zooms in at
00:15:39.440
validations something familiar she
00:15:42.120
decides to see if she can find where the
00:15:43.720
code is written for the presence
00:15:45.160
validation that she's using in her app
00:15:47.560
after all the real so code both
00:15:49.560
seemingly magical and mysterious is also
00:15:51.759
just code written by humans or AI one
00:15:54.480
can never be too sure these days so he
00:15:57.319
finds the validation stutter B file
00:15:59.480
that is included in B.B and heads there
00:16:03.120
in validations RB Zoe searches for the
00:16:05.720
presence keyword not wanting to get too
00:16:08.199
sidetracked she eventually finds it at
00:16:10.319
the end and it seems this too is being
00:16:12.800
required I look to the left and Zoe is
00:16:15.279
able to find pres. RB in the validations
00:16:18.519
folder presents. RB is the end of the
00:16:21.240
line made it Zoe reads the comments and
00:16:24.360
finds them quite helpful for
00:16:25.839
understanding how the class works at a
00:16:27.399
very high level and it's surprised at
00:16:29.480
how little code is actually in this
00:16:31.839
class that was a big old detour but Zoe
00:16:34.560
is pleased with her increased
00:16:36.000
understanding of the rail source code it
00:16:37.720
wasn't as scary as she thought it would
00:16:39.120
be she adds investigate face. RB in more
00:16:42.120
detail to her very large studio list and
00:16:44.120
gets back on
00:16:45.240
track so a recap on model generation Zoe
00:16:49.319
used rail's rail's handy generate
00:16:51.839
scuffle command to create a new model
00:16:53.800
which created all the files she needed
00:16:55.800
and then
00:16:57.600
s then she took a look at her idea class
00:17:01.000
and saw that it was inheriting from
00:17:02.680
application record when she looked at
00:17:04.919
application record she saw that it was
00:17:06.760
inheriting from active record
00:17:08.679
Bas then she V ventured back into the
00:17:11.559
rail source code and saw that base. RB
00:17:13.919
includes or extends all the files that
00:17:16.160
bring in the functionality she uses on
00:17:18.280
The Daily when working with her models
00:17:19.959
and databases she could have stopped
00:17:22.039
there she found the base literally for
00:17:24.199
active record but being curious she
00:17:26.799
delved further in wanting to deeper her
00:17:29.160
understanding of validations using the
00:17:31.320
validation she's using in her model and
00:17:33.000
found herself in validations da B but
00:17:35.799
she wasn't at the end yet as in
00:17:37.600
validations RB it was requiring the code
00:17:40.960
to use to validate presence so she ended
00:17:43.000
up in presents. RB where the magic
00:17:46.840
happens Zoe is feeling like she has a
00:17:49.240
much better understanding of the role
00:17:50.919
active record is playing in her web
00:17:53.120
application and how it comes out of the
00:17:54.799
box with rails in her jump file
00:17:57.440
exploration Zoe learned that rails is
00:17:59.360
actually a gem that is included in the
00:18:00.880
gem file and importantly one of the
00:18:03.559
dependencies of the Rails gem is the
00:18:05.520
active record gem then it was an
00:18:08.400
application.rb where require raila
00:18:12.159
loaded all the components required which
00:18:14.400
includes most importantly for Zoe active
00:18:16.640
Record She also saw that her application
00:18:18.960
class inherits from rails application
00:18:21.200
which gains access to all the
00:18:22.520
functionality provided by the rails
00:18:24.000
framework for managing the application's
00:18:25.919
configuration and
00:18:27.760
behavior database configuration Zoe
00:18:30.360
realized that there's nothing specific
00:18:31.840
to active record but that you do need a
00:18:33.919
database adapter adapter for active
00:18:35.960
record to be able to connect to the
00:18:37.880
database and finally in model generation
00:18:40.760
it was an application record which
00:18:42.559
inherits from active record base that
00:18:44.960
turns what would be a regular Ruby class
00:18:47.080
into an active record bottle when Zoe
00:18:50.080
investigated active record base in
00:18:51.919
source code she saw all the logic that
00:18:54.080
is included or extended in the class and
00:18:56.480
realized that active record is so much
00:18:58.200
more than just a translator between her
00:18:59.919
Ruby code and her SQL like
00:19:02.039
d this marks the end of Zoe's active
00:19:04.400
record learning for now she's excited to
00:19:06.960
spend more time working on her ideas app
00:19:09.000
with her improved understanding of
00:19:10.720
what's Happening behind the scenes she's
00:19:12.960
also got a big old list of items to
00:19:14.559
learn more about that she created on
00:19:16.120
this active record Journey thanks
00:19:17.919
everyone for listening