Demo 7: Database Management Using Rails MVC Models
▶️ Play from the beginning (~1 hour)
In this demonstration, I will explain how to use MVC model classes in Rails to manage a database backend.
Before we dive in with some common model tasks, let’s review a diagram of the Rails architecture.
- ▶️ Prep: Review the README page for the
annotate
Gem (see https://github.com/ctran/annotate_models).
- ▶️ Add the
annotate
Gem to the project Gemfile
.
- ▶️ Download and install the Gem (see
bundle install
).
- ▶️ Configure
annotate
to automatically run with new migrations (see rails g annotate:install
).
- ▶️ Check in changes: Stage, commit, and push these changes to the Git repo (see
git add -A
, git commit ...
, and git push
).
▶️ Check-in Changes: changeset, snapshot
▶️ 2. Creating a Model Class
- ▶️ Prep: Review a class diagram of the model class to be created.
- ▶️ Generate a model class, a database migration, and a test skeleton (see
rails g model ...
).
- ▶️ Inspect the files that were generated.
- ▶️ Initialize the database (see
rails db:migrate
).
- ▶️ Inspect the model file to see the annotations that were added.
- ▶️ Inspect the schema file to see the generated code.
- ▶️ Inspect the SQLite database to confirm that it was generated (see the Firefox SQLite Manager Add-on).
- ▶️ Check in changes: Stage, commit, and push these changes to the Git repo (see
git add -A
, git commit ...
, and git push
).
▶️ Check-in Changes: changeset, snapshot
▶️ 3.1. Using the Rails Console to Execute Model Code
- ▶️ Launch the Rails Console (see
rails c
). This will launch an interactive console that enables the user to execute the Rails model code.
▶️ 3.2. Creating Records in the Database
- ▶️ Create a record using the class method
create
.
- ▶️ Inspect the database to confirm that the record was created (see the Firefox SQLite Manager Add-on).
- ▶️ For purposes of comparison, create a record using the class method
new
and the instance method save
methods.
- ▶️ Inspect the database to confirm that the record was created (see the Firefox SQLite Manager Add-on).
- ▶️ Create several more records using either of the above two approaches.
▶️ 3.3. Retrieving All Records in the Database
- ▶️ Retrieve all records from the database (see the class method
all
).
- ▶️ Print the contents of each record (see the
each
method and the model attribute methods).
▶️ 3.4. Retrieving Records by ID
- ▶️ Retrieve one record from the database based on its ID (see the class method
find
).
- ▶️ Print the contents of the record (see the model attribute methods).
▶️ 3.5. Retrieving Records by Attribute Matching
- ▶️ Retrieve the first record with a particular attribute value (see the class method
find_by
).
- ▶️ Print the contents of the record (see the model attribute methods).
▶️ 3.6. Updating Records
- ▶️ Retrieve a record to be updated by ID from the database (see the class method
find
).
- ▶️ Update the value of an attribute of the record using the
update
method.
- ▶️ Inspect the database to confirm that the record was updated (see the Firefox SQLite Manager Add-on).
- ▶️ For purposes of comparison, update another attribute value using the attribute setter method and the
save
method.
- ▶️ Inspect the database to confirm that the record was updated (see the Firefox SQLite Manager Add-on).
▶️ 3.7. Deleting Records
- ▶️ Retrieve a record to be deleted by ID from the database (see the class method
find
).
- ▶️ Delete the record using the
delete
method.
- ▶️ Inspect the database to confirm that the record was updated (see the Firefox SQLite Manager Add-on).
▶️ 3.8. Exiting the Rails Console
- ▶️ Exit out of the Rails Console (see
exit
).
▶️ 4. Resetting the Database
- ▶️ Reset the database, deleting its contents and re-initializing it (see
rails db:reset
).
- ▶️ Inspect the database to confirm that it was reset (see the Firefox SQLite Manager Add-on).
▶️ 5. Seeding the Database with Data
- ▶️ Add code to
db/seeds.rb
to seed the database with several records (see create!
).
- ▶️ Reset the database and seed it (see
rails db:reset
).
- ▶️ Inspect the database to confirm that it was reset and seeded (see the Firefox SQLite Manager Add-on).
- Check in changes: Stage, commit, and push these changes to the Git repo (see
git add -A
, git commit ...
, and git push
).
Check-in Changes: changeset, snapshot