Create a new model class and migrate the database

❮ Back Next ❯

In this step, you will learn how to generate a new model class and initialize the database for saving objects of that class.

In VS Code, inspect the contents of the app/models/ folder.

Note that the folder does not contain any model classes. It contains only a subfolder concerns (which we will not use) and a Ruby code file application_record.rb, which contains a base class inherited by all model classes.

In VS Code, inspect the contents of the db/ folder.

Note that the folder does not contain a migrate/ folder (and thus, no migration scripts). It contains only the Ruby code files schema.rb (an encoding of the database schema that is generated by Rails) and seeds.rb (a place for us to add code that seeds the database with records).

In the terminal, run this PostgreSQL command to list the tables currently in the practice app’s database:

psql --command="\dt" practice_app_development

Note that only two tables are listed (ar_internal_metadata and schema_migrations). Both of these tables are created and used by Rails by default.

If the above psql command locks the terminal while displaying its output, hit the Q key on your keyboard to exit the command and get a new terminal prompt.

Consider this model class design for representing people:

Generate this model class and a corresponding database migration script:

rails generate model Person first_name:string last_name:string height_inches:integer weight_lbs:integer

In VS Code, inspect the contents of the app/models/ folder.

Note that the folder now contains a Ruby code file person.rb that contains the Person model class.

In VS Code, inspect the contents of the db/ folder.

Note that the folder now contains a migrate/ subfolder, and within that subfolder is Ruby code file with a name similar to 20240203174351_create_people.rb (the numbers are a timestamp which will vary depending on when the file was generated). This code file is a migration script for setting up the database. Inside the code file, notice a call to the Rails method create_table for adding a new table to the database.

Run this migration to configure the database:

rails db:migrate:reset

In the terminal, run this PostgreSQL command to list the tables currently in the practice app’s database:

psql --command="\dt" practice_app_development

Note that a table people has been added to the database. This table corresponds to the model class Person. Rails automatically names a database table as the snake_case, plural form of the class name (thus, Person is translated to people).

In the terminal, run this PostgreSQL command to inspect the schema for the database table people:

psql --command="\d people" practice_app_development

Note that each column of the table people is listed. It has a column for each of the model class attributes we specified above (first_name, last_name, etc.). It also has three columns automatically added by Rails. The id attribute is a unique ID number for each table row. The created_at and updated_at attributes are timestamps that are automatically set to record when each row was first created and last updated, respectively.

In the terminal, run this PostgreSQL command to inspect the rows of the table people:

psql --command="SELECT * FROM people" practice_app_development

Note that the table contains 0 rows, because we have not yet created any model objects and save them to the database.

In VS Code, inspect the Ruby code file app/models/person.rb.

Note that the file contains the definition of the class Person. The class definition itself is essentially empty (no mention of the attributes). Rails automatically adds the appropriate attributes to the class based on the database schema. The comments that precede the class definition list these attributes for the convenience of the developer. (These comments were generated by the Annotate gem.)


❮ Back Next ❯