Hello and welcome.
In this lecture we are going to create the model for our post at the moment.
We are not able to submit the form to a database.
So by creating a model to enable us to do that.
So this is what our form of mission looks like at the moment.
At the moment we're with some in the form is just going to render the in parameters on the screen as
we can see here.
So all these different database that comes with reals is askew a light you can use at any database you
want and does really matter obscure serve my school posek cress any database will work the same way.
Let's have a look at where database settings are defined in this application.
So as usual he will be in the configuration fast.
So if we look down here there's a config directory and inside this conflict derrek FREE there's a database
doorway air fast.
This is where the database settings are defined you can see here's his default.
You've got a default adapter which is priscu a light if you were in it tells you he also database the
be obscure light in development so if he's in my secure for example this is where you would define if
you were to use my secure database you would first have to install databases on your development machine
and then you have to come in here and change the settings to reflect what database you are using because
we're going to use a default or the settings is already in place.
We don't need to change anything or open up your terminal or command line so we can create a model for
the post.
So if you're using a badge like me if you want to colour your screen you're just typing Claire.
And that should clear your screen.
So what we're going to do now is actually going to generate a model.
It is a model that will interface with the database.
So it's a great model.
Just type in Been slash rails slash.
You can either type in the forward generate or just type in G generate or you can type in G for short
to do the same thing.
They need to specify what you are generating.
You say a model and then you.
When you're creating models models are single in compare the two.
When you are creating controllers controllers are plural.
So how about creating a model called POLST.
Moses I think the post then you need to specify the field for the model for the field in the database
or the way you generate a model you give it a name that is singler but the corresponding tables that
is created can be plural will be plural.
So we need to define the fields for the module.
So he's going to have a title so I typed in title and they put in the quoll on the title is going to
be is freeing data type.
Okay do a space is also going to have a body and do a call on the body is going to be a text.
So that is basically it.
What we need to generate the model.
So what this command is saying that we are telling reals that we want.
He pulls to model together with their title attribute of the types shrink and a body attribute of the
type text.
So these attributes are automatically added to the post table in the database and they will be mapped
to the portrait model.
So press enter.
And that's huge.
Generate the model so reals has kindly responded to the request and is created a bunch of files here.
The ones we are the only one we interested in here is it up model slash post derby which is this one
here.
This one here this up models last post not hard and this one here we're interested in this too.
That one and that one.
So when you generate it model it.
But it's this far.
Okay so the father there.
But you need to run the migration for the database tables to be created a list of table not being created.
So you just have to go in and run the command that will create the table but we can take a look before
we run the migrates command active record which is this here.
This active work on the active record is quite smart.
It will automatically map mccollom names to the module attributes.
Okay so the attributes were given in model the active recall automatically manacle on names to a do
module attributes which means you don't have to declare that Ruby was inside the reals model as that
will be done automatically for you by the active record so to look at the file those create it where
we generated the model.
So we come here and look at.
This should be an option for the B.
Look inside it dbe directory expand that it tells you anywhere where you create the model it tells you
where it tells you the part.
Our models Polestar B and here dbe migrants so we're looking at this one now dbe migrate which is here
which is this one here.
So basically this file here.
This is why the active record discrete.
This is a table that's going to be create it's going to create a table quad post which is plural and
the table will have this with this field.
You have a title and a body.
It also gives you a timestamp So when you generate or when you run the migration it all include a time
stamp which means you can always revert back if you look here.
You see the date this has the time stamp so when you are looking for or wrong migration it all generate
a time stamp.
You can use to reference back as it is now.
This for us.
To run this.
This is an active migration we need to run this for the table to be created so all we need to do is
come into this command line here and we type in being slas braille's space need be migrate.
Press Centre.
And that will generate a migration that will create this table quote post with the title body and you
have two fields quote one will be Title One will be body so you can see the migration has been executed.
You can tell us here here that the what.
It is my great head that it created the table called post in this time and this is the.
This is when it was migrated so it give you a time stamp the time stamp enables you to roll back out
if you've had if you've done several migrations you can easily tell which ones you've you.
By the time stamp one are referred back so we now have our table created.
You can see the migration has been executed successfully.
So that's it for this collection now that we've caught our model created.
We can now communicate with the database in our next lecture or we are going to see how we can see data
in the controller.
Thanks for watching.
And by phone again sorry this show again has gone a long time to you know do as much as possible to
explain it the best way you can so that you can get it too if you do have any issues.
Please let me know.
Thanks for watching.
And by follow.