This part is about the model M in MVC.
ActiveRecord is the core component of the database operation of rails, using the standard ORM model, mapping the tables of the database to the appropriate classes, matching the row records to the specific instance objects, and the fields corresponding to the properties of the instance objects.
The ORM model is the high-level encapsulation of the database, which allows us to manipulate the database without using complex SQL statements. And so we don't have to worry about whether the database is MySQL or sqlite, all the same to call the interface can be. The disadvantage of ORM is not flexible enough, sometimes affect the efficiency of database storage, but efficiency we do not consider, we consider the rapid development can be.
The following is for expansion:
A ActiveRecord is a model class that corresponds to a table of a database
For example, if we create an instance object with ActiveRecord, the instance object often corresponds to a row of records in this table (or multiple rows of records).
A property in an instance object corresponds to a field in the row.
Continue to refine our Weibo examples below:
As above, rails auto-generated post model has nothing, it inherits from the ActiveRecord class, and this class itself encapsulates a lot of database operation methods, such as the following post.all is ActiveRecord database operation method. The post in the controller file is the model class above, so the model class has no code and can be used directly in the controller file (all encapsulated).
The following posts variable is the instance object, which is the row record (this is multiple lines)
Rails official documentation, using examples such as the Post.all all method in ActiveRecord, in the document , that is, querying all the data in the model:
The 14th row in the Index view is the field, that is, the content field for each record or the content property of the model instance.
We also need the user model, the user has the registration, the release microblogging function and so on, because the function is many, we actually want to add is the user resource (Controller, the view, the Model A lot of all contains), but we say here is the model, we create a user model well, the controller those back plus go also the same. To create a model or use the Generate command, note that creating a resource we use the plural, but the controller or model is in the singular, the following is the user instead of the users
We create two fields for the user table, one for the username type string and one for the e-mail type string
After the model is created, the database migration operation is performed first, and the rake Db:migrate can be entered:
This is when the user model is really available. Because the user is not added to the corresponding controller, view, model, so we can not do on the page before the operation, only the model we first in the Rails console operation (not the CMD console on the line). We want to get into the rails console, (remember that you have to execute Rails console in the project catalog) as follows
So we can use the rails console, and we'll use rails to create a user named Sundi:
Enter the first line of the command and click Enter
As above, we have created the user object in memory (the table is the user table, only you are capitalized here), but we have not saved the object to the variable and cannot manipulate the object. So we first save the object to the variable user
@user =user.new (:username=> ' Sundi ',:email=> ' [email protected] ')
Note that the Rails ' console line is full and will not be wrapped, but the following is the > symbol, as in the statement above:
The results are as follows
This is not enough, we also need the model's Save method to actually create this object into the user table:
After creation, you can use the user model's Find method to view:
The incoming parameter is the ID number created because we created only one user Sundi so the ID is definitely 1.
You can see that the returned results also include the creation time and the update time, which is quite intelligent.
We look at all by using the All method, only 1 so the return result looks the same, but remember that the data type of the returned result is different
With this lesson, we have a basic understanding of the concepts of the model component ActiveRecord class in Rails and a basic understanding of how ActiveRecord is used by creating a user model.
9--rails Data Interaction 1