AR Example: Todo

Todo List

My first exposure to the todo list challenge was during the time we at DBC had to practice ARGV in ruby(which was also the first time we learned how to use parameters on the console). But that was before we learned databases and Active Record. This is walkthrough on the workflow of solving todo using Active Record. We will be using the following folders:(Look at my previous post for more info on each folder)

Workflow

This workflow follows the standard set in my previous blog on workflows. We start with making the database, figuring out the tables and schemas, and then making the application.

In our file, we will need something that can link our ruby files to our database. This is Active Record. The actual connection itself for this project is stored in our config folder, under application.rb The most important line in this file is found on line 26. This establishes the connection between ActiveRecord and the database. (Active record is an ORM bundled into a gem)

Step 0: Create source file

When I say source file, I mean the file that you will run to make your entire application run. This should be found within the source directory of your project. In our case, it is called todo.rb All This file should do is run a command in our master controller(line 4). To do that, it needs access to the controller.(line 1)

Step 1: MVP and design schema

So I cheated. This is actually 2 steps bundled into 1. You need to design your database table schema based upon what you think you will need. But don't make it too complex, you should follow the MVP(minimum viable product.) You can always make more migrations to add columns to a table and join tables together.

Step 2: Make the migration file.

The migration file is how you will implement your table and schema into your database. It will require access to the database itself via Active Record.(line 1) Naming convention of migration file should be snake_case lowercase while class should be CamelCase capitalized. The file here should be plural since we are creating a table full of tasks.

Name of the class should reflect what you are doing to the database. In this case, we are creating a table Tasks.(line 3)

Optional: Seed your database table

Once you have a database with tables, you can seed your tables with initial data.

All steps after deal with the application itself.(Inside the app directory)

Step 3: Create corresponding model

After creating your migration file, make the corresponding model. Your migration file only deals with the database. You need to make something that deals with the ruby side. The model should be of the same name as the table you are assigning it to, and singular. It will act as one object that you can pull out of the database table. Therefore, it will also need access to Active Record.(line 1, line 3 in inheriting AR)

Step 4: Make controller

Make your controller. This is the brains of your application. If you remember Step 0, this is what your are calling when you run your program. Make sure to require_relative your models since those are the things you are manipulating here. The example below isn't the best MVC example of a controller, because you should be creating a seperate View anytime you see a "puts" and require_relative the corresponding "puts".