This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Ruby on Rails 5 Basics!
You have completed Ruby on Rails 5 Basics!
Preview
The model is responsible for storing and retrieving data from users of your app.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Here we are, back on the rails logs
showing the processing of our request.
0:00
This language generated by the post model.
0:04
The model is responsible for storing and
retrieving data from users of your app.
0:07
Model classes or Ruby classes
that usually, though not always,
0:11
know how to read and
write data from a database.
0:15
Those classes each have one or
0:18
more attributes that hold data
you need to keep track of.
0:19
So, if you were creating an airline
booking app, for example, you might have
0:22
a flight model class with flight
number and departure time attributes.
0:26
As well as a passenger model class,
with name and seat assignment attributes,
0:30
among many others.
0:34
You can create an instance of a model
class, set its attributes and then,
0:36
call the Save method on it to write its
data to the database as a new record.
0:40
Later on, you can call various methods to
retrieve those records from the database,
0:45
each record gets returned as
a separate instance of the model class.
0:49
So, here's how we're using the model
class to show a list of all
0:54
posts in our blog app.
0:57
The controller's index method has
the line @post equals Post.all.
0:58
Post is the model class
representing this resource.
1:03
Rails creates the source code for model
classes in the app models sub directory.
1:06
Here's the post class in post.rb.
1:13
Again, it doesn't look like much, but
1:16
that's because post is a subclass
of the application record class.
1:18
Prior to Rails 5, it would have been
a subclass of active record base, and
1:22
in fact, it still is behind the scenes.
1:26
The all method that
are controller calls and
1:29
many other methods are inherited
from Application Record.
1:32
The all method retrieves all of the post
objects stored in the database.
1:36
It generates this query.
1:41
This is in structured query language or
SQL,
1:43
a language used to
communicate with databases.
1:46
Rails generates SQL queries for you.
1:50
This particular query loads the data from
all of the columns in all of the records
1:52
in the post database table.
1:56
Rails takes each database record, creates
an instance of the post Ruby class,
1:59
and uses the database column values to
set the attributes of the post object,
2:04
Post.all returns a collection of these
post objects which we then store in
2:09
the posts instance variable.
2:14
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up