Ruby on Rails and editing your Models.

When I first started learning about rails and how models worked, I was terrified that if I left something out of my initial model migration, I would have to start over from scratch and do it over. Luckily with much more practice and trial and error, I’ve learned that certainly isn’t the case. Lets talk about creating a user model in Rails and how to add columns of data to your model after you’ve migrated it:

After spinning up your new rails app with:

$ rails new project_name

Make sure to switch to the new folder:

$ cd project_name

Now lets make a data base table called Movie, and for now we’ll include two columns only:

$ rails g model Movie title:string director:string

After using the model generator, Rails will create several files, more importantly at the moment lets look at db/migrate/<timestamp>_create_movies. In this newly created file you’ll see we have the following:

class CreateMovies < ActiveRecord::Migration[6.1]
def change
create_table :employees do |t|
t.string :title
t.string :director


Luckily for us, Rails created our two columns of title and director which we specified earlier. Before we go any further, we need to make sure we complete our migration with the following command, which should return a response of create_table in our terminal:

$ rails db:migrate

After we initiated our migration we noticed we made a mistake and forgot to add two very important columns to our movie table. These were the mistakes that almost always lead me to abandon my new Rails project and start all over by creating a new one. I saw the absurdity in this and forced myself to stop being a big baby. Thank goodness I did, because now I know what to do! Soon you will too.

To help alleviate this pickle we’re in, lets go ahead and add two more columns to our table all at once:

rails generate migration AddDetailsToMovies sequel:boolean year_released:string

This will produce the following migration:

class AddDetailsToMovies < ActiveRecord::Migration[6.1]
def change
add_column :movies, :sequel, :boolean
add_column :movies, :year_released, :string

Last step will be to make sure and migrate this again. Then you’ll have two shiny new columns added to your Movie model. Just to double check that you didn’t fat finger anything, go ahead and slide into your db/migrate/schema file to see that your new entries were added. Now go migrate some stuff.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Infinite number counting machine

How to start contributing to Hugging Face 🤗 2021

[Flutter]: How to configure application in the cloud by Firestore “GetX + DIO + Firebase”

How to apply a Word DOCX Template in PHP

7 ways for the beginner programmer to become better, faster

The Ultimate Python Guide: How to become a coding god

Understanding My Stock Program

How to Create a Custom Staggered Animation with Flutter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kyle Davis

Kyle Davis

More from Medium

Distributed caching with Ruby on Rails

Distributed caching with Redis and Ruby on Rails

Build a Ruby .DevContainer for Development

Ruby on Rails: Still on Track in 2022?

The Repository Pattern in Ruby (with the Active Record library)