Web Development with Rails and Git Tutorial

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.

  1. ▶️ Prep: Review the README page for the annotate Gem (see https://github.com/ctran/annotate_models).
  2. ▶️ Add the annotate Gem to the project Gemfile.
  3. ▶️ Download and install the Gem (see bundle install).
  4. ▶️ Configure annotate to automatically run with new migrations (see rails g annotate:install).
  5. ▶️ 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

  1. ▶️ Prep: Review a class diagram of the model class to be created.
  2. ▶️ Generate a model class, a database migration, and a test skeleton (see rails g model ...).
  3. ▶️ Inspect the files that were generated.
  4. ▶️ Initialize the database (see rails db:migrate).
  5. ▶️ Inspect the model file to see the annotations that were added.
  6. ▶️ Inspect the schema file to see the generated code.
  7. ▶️ Inspect the SQLite database to confirm that it was generated (see the Firefox SQLite Manager Add-on).
  8. ▶️ 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. Performing CRUD Operations on the Model

▶️ 3.1. Using the Rails Console to Execute Model Code

  1. ▶️ 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

  1. ▶️ Create a record using the class method create.
  2. ▶️ Inspect the database to confirm that the record was created (see the Firefox SQLite Manager Add-on).
  3. ▶️ For purposes of comparison, create a record using the class method new and the instance method save methods.
  4. ▶️ Inspect the database to confirm that the record was created (see the Firefox SQLite Manager Add-on).
  5. ▶️ Create several more records using either of the above two approaches.

▶️ 3.3. Retrieving All Records in the Database

  1. ▶️ Retrieve all records from the database (see the class method all).
  2. ▶️ Print the contents of each record (see the each method and the model attribute methods).

▶️ 3.4. Retrieving Records by ID

  1. ▶️ Retrieve one record from the database based on its ID (see the class method find).
  2. ▶️ Print the contents of the record (see the model attribute methods).

▶️ 3.5. Retrieving Records by Attribute Matching

  1. ▶️ Retrieve the first record with a particular attribute value (see the class method find_by).
  2. ▶️ Print the contents of the record (see the model attribute methods).

▶️ 3.6. Updating Records

  1. ▶️ Retrieve a record to be updated by ID from the database (see the class method find).
  2. ▶️ Update the value of an attribute of the record using the update method.
  3. ▶️ Inspect the database to confirm that the record was updated (see the Firefox SQLite Manager Add-on).
  4. ▶️ For purposes of comparison, update another attribute value using the attribute setter method and the save method.
  5. ▶️ Inspect the database to confirm that the record was updated (see the Firefox SQLite Manager Add-on).

▶️ 3.7. Deleting Records

  1. ▶️ Retrieve a record to be deleted by ID from the database (see the class method find).
  2. ▶️ Delete the record using the delete method.
  3. ▶️ Inspect the database to confirm that the record was updated (see the Firefox SQLite Manager Add-on).

▶️ 3.8. Exiting the Rails Console

  1. ▶️ Exit out of the Rails Console (see exit).

▶️ 4. Resetting the Database

  1. ▶️ Reset the database, deleting its contents and re-initializing it (see rails db:reset).
  2. ▶️ Inspect the database to confirm that it was reset (see the Firefox SQLite Manager Add-on).

▶️ 5. Seeding the Database with Data

  1. ▶️ Add code to db/seeds.rb to seed the database with several records (see create!).
  2. ▶️ Reset the database and seed it (see rails db:reset).
  3. ▶️ Inspect the database to confirm that it was reset and seeded (see the Firefox SQLite Manager Add-on).
  4. 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

© Scott D. Fleming 2018 • Made with GitHub Pages and Markdown