1 00:00:00,800 --> 00:00:04,500 Here we are, back on the rails logs showing the processing of our request. 2 00:00:04,500 --> 00:00:07,260 This language generated by the post model. 3 00:00:07,260 --> 00:00:11,610 The model is responsible for storing and retrieving data from users of your app. 4 00:00:11,610 --> 00:00:15,170 Model classes or Ruby classes that usually, though not always, 5 00:00:15,170 --> 00:00:18,140 know how to read and write data from a database. 6 00:00:18,140 --> 00:00:19,650 Those classes each have one or 7 00:00:19,650 --> 00:00:22,740 more attributes that hold data you need to keep track of. 8 00:00:22,740 --> 00:00:26,140 So, if you were creating an airline booking app, for example, you might have 9 00:00:26,140 --> 00:00:30,650 a flight model class with flight number and departure time attributes. 10 00:00:30,650 --> 00:00:34,600 As well as a passenger model class, with name and seat assignment attributes, 11 00:00:34,600 --> 00:00:35,570 among many others. 12 00:00:36,760 --> 00:00:40,330 You can create an instance of a model class, set its attributes and then, 13 00:00:40,330 --> 00:00:45,110 call the Save method on it to write its data to the database as a new record. 14 00:00:45,110 --> 00:00:49,880 Later on, you can call various methods to retrieve those records from the database, 15 00:00:49,880 --> 00:00:53,060 each record gets returned as a separate instance of the model class. 16 00:00:54,350 --> 00:00:57,230 So, here's how we're using the model class to show a list of all 17 00:00:57,230 --> 00:00:58,790 posts in our blog app. 18 00:00:58,790 --> 00:01:03,700 The controller's index method has the line @post equals Post.all. 19 00:01:03,700 --> 00:01:06,900 Post is the model class representing this resource. 20 00:01:06,900 --> 00:01:13,460 Rails creates the source code for model classes in the app models sub directory. 21 00:01:13,460 --> 00:01:16,400 Here's the post class in post.rb. 22 00:01:16,400 --> 00:01:18,360 Again, it doesn't look like much, but 23 00:01:18,360 --> 00:01:22,880 that's because post is a subclass of the application record class. 24 00:01:22,880 --> 00:01:26,960 Prior to Rails 5, it would have been a subclass of active record base, and 25 00:01:26,960 --> 00:01:29,590 in fact, it still is behind the scenes. 26 00:01:29,590 --> 00:01:32,060 The all method that are controller calls and 27 00:01:32,060 --> 00:01:35,090 many other methods are inherited from Application Record. 28 00:01:36,330 --> 00:01:39,850 The all method retrieves all of the post objects stored in the database. 29 00:01:41,680 --> 00:01:42,870 It generates this query. 30 00:01:43,870 --> 00:01:46,950 This is in structured query language or SQL, 31 00:01:46,950 --> 00:01:50,160 a language used to communicate with databases. 32 00:01:50,160 --> 00:01:52,910 Rails generates SQL queries for you. 33 00:01:52,910 --> 00:01:56,830 This particular query loads the data from all of the columns in all of the records 34 00:01:56,830 --> 00:01:59,450 in the post database table. 35 00:01:59,450 --> 00:02:04,448 Rails takes each database record, creates an instance of the post Ruby class, 36 00:02:04,448 --> 00:02:09,299 and uses the database column values to set the attributes of the post object, 37 00:02:09,299 --> 00:02:14,000 Post.all returns a collection of these post objects which we then store in 38 00:02:14,000 --> 00:02:15,894 the posts instance variable.