Model Creation & Management

Free Ruby on Rails Tutorial

Dive into this in-depth tutorial on Ruby on Rails, exploring topics such as generating and rolling back a migration, and updating views and controllers to match an updated model.

This exercise is excerpted from Noble Desktop’s past web development training materials. Noble Desktop now teaches JavaScript and the MERN Stack in our Full Stack Development Certificate. To learn current skills in web development, check out our coding bootcamps in NYC and live online.

Topics Covered in This Ruby on Rails Tutorial:

Generating & Rolling Back a Migration, Updating Views & Controllers to Match an Updated Model

Exercise Preview

model management exercise preview

Exercise Overview

In this exercise, we will add more columns to the movie model and update the views and controllers to reflect these additions.

Full-Stack Web Development Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.
  1. If you completed the previous exercises, you can skip the following sidebar. We recommend you finish the previous exercises before starting this one. If you haven’t finished them, do the following sidebar.

    If You Did Not Do the Previous Exercises (3A–4B)

    1. Close any files you may have open.
    2. Open the Finder and navigate to Class Files > yourname-Rails Class
    3. Open Terminal.
    4. Type cd and a single space (do NOT press Return yet).
    5. Drag the yourname-Rails Class folder from the Finder to the Terminal window and press ENTER.
    6. Run rm -rf flix to delete your copy of the Flix site.
    7. Run Git clone https://bitbucket.org/Noble Desktop/flix.Git to copy the Flix Git repository.
    8. Type cd flix to enter the new directory.
    9. Type Git checkout 4B to bring the site up to the end of the previous exercise.
    10. Run bundle to install any necessary gems.
    11. Run yarn install—check-files to install JavaScript dependencies.

Getting Started

  1. Close all of the files you have open in your code editor.

  2. Open the Finder and navigate to Class Files > yourname-Rails Class

  3. Open Terminal.

  4. Type cd and a single space (do NOT press Return yet).

  5. Drag the flix folder from the Finder to the Terminal window.

  6. Make sure you’re in Terminal and hit Return to change into the new folder.

Generating & Rolling Back a Migration

We created a movie model a few exercises back—does this mean that the movie model is fixed, immutable, unable to change? Of course not! In fact, the Editorial Department at Flix has asked us to add a new field for director to the Movie Details sidebar on each of the detail pages.

We already have a model, so we’re not going to call rails generate model again. Instead, we’ll call rails generate migration. Let’s try it out.

  1. Type the following, but do NOT press Return yet!

    rails generate migration

    We can generate just a migration file by itself to apply changes to an existing model. Let’s give it a name that reflects the changes we’re making.

  2. Add the following name and field-type to the command and press Return after typing to apply the migration:

    rails generate migration add_director_to_movies director:text

    Terminal will reply letting you know that a new migration file has been created.

  3. Switch to your code editor.

  4. Open flix > db > migrate > #_add_director_to_movies.rb (the # is a placeholder for the file’s unique timestamp).

  5. Take a look at this code in the file:

    def change
       add_column :movies, :director, :text
    end

    How did Rails know that we wanted to add this to the movies model? Because we crafted the name of the migration in a certain way, naming it add_X_to_movies. Rails is smart enough to parse the to_movies part at the end of the string, recognize that it matches an existing model and construct a migration in such a way that the movie model will get a new column.

  6. We’re not going to change anything in this file, so let’s go ahead and apply the migration. Switch to the Terminal.

  7. Type the following to apply the migration:

    rails db:migrate

    Terminal should confirm with a message including add_column to let us know that the field was added successfully—except, wait! We don’t want director to be a text area, it should just be a regular string. What happens now? Are we screwed? Do we have to delete the database and start over? Of course not. All we have to do is roll back the migration and then edit it.

  8. Type the following in Terminal:

    rails db:rollback

    Terminal should reply that it is reverting and it has performed a remove_column action, which is clearly the opposite of add_column.

  9. Let’s fix our messed-up migration. Switch to #_add_director_to_movies.rb in your code editor.

  10. Make the following change shown in bold, replacing text with string:

    def change
       add_column :movies, :director, :string
    end
  11. Save the file.

  12. Switch to Terminal.

  13. Type the following:

    rails db:migrate

    Great, the fixed migration (with director now as a string) has been applied!

Updating Views & Controllers to Match an Updated Model

  1. Switch to your code editor.

  2. Open flix > app > views > movies > _form.html.erb

  3. Add the following code around line 14, after the has_subtitles part of the form:

    <%= f.label :has_subtitles %>
       <%= f.check_box :has_subtitles %>
    </p>
    <p>
       <%= f.label :director %>
       <%= f.text_field :director %>
    </p>
  4. Save the file.

  5. Open flix > app > controllers > movies_controller.rb

  6. Scroll down to the bottom of the file. Inside the private movie_params method in the long line of code, add the following bold code (keeping it on one line):

    movie_params = params.require(:movie).permit(:title, :description, :has_subtitles, :placement, :mpaa_rating, :release_date, :ticket_price, :runtime, :poster_image, :director)

    If you were to forget to add these to the movie_params, the new fields wouldn’t be saved when you saved the form. This is a common thing to forget to change when updating a form!

  7. Save the file.

    Let’s make sure that the new fields show up in the movie details view.

  8. Open flix > app > views > movies > show.html.erb

  9. Around line 15 add the following bold code:

    <div class="details">
       <h4>Movie Details</h4>
       <div>
          <span></span>
       </div><div>
          <span>Subtitles:</span>
  10. Add the following bold code:

    <div class="details">
       <h4>Movie Details</h4>
       <div>
          <span>Director:</span>
          <%= @movie.director %>
       </div>
       <div>
          <span>Subtitles:</span>
  11. Save the file.

  12. Type the following to launch the server:

    rails server
  13. Switch to the browser, navigate to localhost:,000/movies/1/edit and check out the new field for director. Let’s fill in this new field.

  14. Fill in the field for director as follows (or invent your own entry if you feel like it!)

    Director: Sam Badams
  15. Click Update Movie.

    The changes should be successfully added to the Movie Details sidebar! Excellent.

Noble Desktop Publishing Team

The Noble Desktop Publishing Team includes writers, editors, instructors, and industry experts who collaborate to publish up-to-date content on today's top skills and software. From career guides to software tutorials to introductory video courses, Noble aims to produce relevant learning resources for people interested in coding, design, data, marketing, and other in-demand professions.

More articles by Noble Desktop Publishing Team

How to Learn Coding

Master Coding with Hands-on Training. Learning How to Code in JavaScript, Python, and Other Popular Languages Can Pave the Way to a Job in Tech Such As Web Development, Data Science & Analytics, or Software Engineering.

Yelp Facebook LinkedIn YouTube Twitter Instagram