Phase 3 - Updating and Dropping Databases using Active Record
Tags: activerecord, codingbootcamp, ruby, rails
Published: 2022-11-11 21:46:49

For this project, we had to build an application that uses Active Record to interact with a database. I decided to go with a task management application for Podcast planning titled **Podtask**. This tool will allow users to document their podcast topics, featured guests, and tasks to complete for a successful episode release. 

One requirement for this project is that the database has to have at least two models with a **one-to-many** relationship.

## One-to-Many Relationships
For context, in relational databases, a _one-to-many relationship_ occurs when a parent record in one table can potentially reference several child records in another table. When I started this project, I created 3 database migrations:

- a table for Podcasts (_Parent_ Record*)
- a table for Tasks (_Child_ Record*)
- and a table for Lists, which was associated with the Podcast ID and a task ID

However, while working through my project, I quickly noticed that I didn't need to use the List table in the database. This is when I had to refresh my memory on how to delete a table, as well as deleting columns from a table. 

### Delete Column from Database Table

To get started, you have to run a new migration for removing the column. The Tasks table we'll be working with first has the following build in it's migration:

```
class CreateTasks < ActiveRecord::Migration[6.1]
  def change
    create_table :tasks do |t|
      t.integer :podcast_id
      t.integer :list_id
      t.string :to_do
      t.string :todo_status
      t.timestamps
    end
  end
end  
```
The column we will need to remove is `list_id`. 
I ran the following code to start the migration, and made sure the name included details about the action we wanted to take place:

```
bundle exec rake db:create_migration NAME=remove_column_from_tasks
```
Once the migration was created, I followed the instructions found on the [Ruby on Rails - Active Record Migrations](https://api.rubyonrails.org/v7.0.4/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-remove_column) online guide. To remove a column, we will be using the `remove_column` statement. 

> **remove_columns**(_table_name, *column_names, type: nil_)

With our statement in place, this is what our migration looks like:

```
class RemoveColumnFromTasks < ActiveRecord::Migration[6.1]
  def change
    remove_column :tasks, :list_id, :integer
  end
end
```
Alright, with everything in place we can run this migration using the following command:

`bundle exec rake db:migrate  
`
Success! By checking the schema.rb file, we can confirm the column was removed. 

### Delete Column from Database Table

For deleting an existing table, we can pretty much follow the same steps above except we would be using the `drop_table` statement instead of the `remove_column` statement.

After creating a migration...
`bundle exec rake db:create_migration NAME=drop_lists_table`

...we can set up our migration with the following build:

```
class DropListsTable < ActiveRecord::Migration[6.1]
  def change
    drop_table :lists
  end
end
```

Lastly, I ran the `bundle exec rake db:migrate` command and checked the `schema.rb` file - success again! Our table was removed.  

### Migrations Are Our Friends
I'll admit, I was a little comprehensive about making adjustments to the database - especially after already building most of project. 

However, I learned that migrations aren't scary. Even with proper planning before starting a project, things happen and there are plenty of resources are your fingertips to guide you through.