Learn how to access models through the Rails console, in order to create new records in the database. There are two approaches to saving records, new/save and create.
- [Instructor] In this movie, we'll use the Rails Console to access our models and to create new records in the database table. I'm going to show you two different techniques to accomplish this. The first, I refer to as new plus save, it's a three-step process. First, we instantiate an object. Instantiate means to create a new object of the class. This object is also called an instance of the class. And then we can set values on that instance. Once we have all the values set, we can save the app to the database. That's the three-step process, instantiate, set the values and save it. The second technique is to do it all-in-one step using create. It has the same effect as new and save, it's just one single command. Create will instantiate the object, set the values and save it. Before we go to the command line and try this out, let's just go into our app, and let's see the models that we've already created. We have models for subject, page and user. Let's click on Subject.rb. You'll see that's just a very simple Ruby class that inherits from ApplicationRecord. Now, ApplicationRecord is also a model here. It's a model without a database table. And what it does is, it inherits from ActiveRecord::Base. You're really not going to put much in ApplicationRecord, it's really only here in case you need to be able to put code that applies to all of your models, because all your models inherit from ApplicationRecord. It's rare that you need that. Instead, what's really happening is that ActiveRecord::Base, all of its behaviors, all of its attributes, are all being passed along to each one of your models, so they have all that behavior, even though they don't have any additional code of their own yet. Let's see some of the behaviors it has. Let's go over to the Command Line. And from the root of my Rails project, I'll type Rails Console to enter the Rails Console. Once we're there, let's create a new subject, subject equals capital subject, and then dot new, that's going to call the new method on subject. And I could just hit Return and create a new instance, but I'm going to put parentheses and then provide a hash of values. Name equals first subject and visible is true. I'll hit Return. And you'll see that it created a new instance of the class. That's step one. Here's my instance. You can see that it went ahead and took those values I provided and set some of the attributes. Now, didn't set all of the attributes, we can also set subject dot position equals one. And we can get back these values as well, name subject to that position. And you can see that those are all set. We can work with these in an object-oriented way. Now, let's try, subject dot new, underscore, record, question mark. This is another one of those built in behaviors in ActiveRecord, and it will tell us whether or not this is a record that has been saved to the database or not. So it has not, it's a new record, not yet saved to the database. We can also type Subject dot ID and see that it hasn't been given an ID yet, which is what we would expect if it was saved to the database. So now, we've done steps one and two, we instantiate an object, we set its values. Now, let's do step three, subject dot save. And there is, it saved it to the database. And we know that because it gave us output of the SQL statement that it created. Notice that it also returns true or false. This is very useful if we want to have a conditional that will perform one action if the save succeeds or another action at the save fails. Now, if we hit the Up Arrow and try subject new record again, it comes back and says, False. It's not a new record, it has been saved to database and we hit the Up Arrow to get subject.id. You can see that now its ID is one. We can also do subjects dot created, underscore at, and subject dot updated at. Those are magic columns that Rails automatically populates for us when it saves a record to the database. So if we have those columns, ActiveRecord will automatically tell us when any record was created and updated. Now, let's look at the second technique using create. Let's do subject again. I'll do subject two this time, so we won't be confusing. Subject equals capital subject again. For my class, I'm going to create and let's also provide some attributes to create. So we'll have name, and this will be second subject, and we'll give it visible equals false. Now this time, it was a one-step process. As soon as I hit Return, this create method that is inherited from ActiveRecord went ahead and created the record of the database or at least it tried to. It didn't return true or false to us this time, instead, it returned the object that it created. And that's useful, we want it to return the object because I'm assigning that to a variable. And then I can ask that variable which contains the instance, Are you a new record? It'll come back and say, No, I'm not a new record. I have been saved to the database, subjecttwo.id. And you can see that it has an ID of two. Now, you know two ways to create new records in the database. We did it from the Console, but you could use the same techniques inside your controllers.