Voicey needs to have users to store Memos. We need to scaffold a basic User model with a name and an email.

Primer on Rails Generators

Rails uses a lot of command line tools to speed up the process of development. They are optional but are widely used and can save you some development time.


Generators enable us to create files quickly. Most of the time, generators will contain boilerplate code that we can use. We will use them to create migrations, models, views and controllers in this tutorial.

Creating the User model

Lets start by creating the User model since we will need a way to associate a Memo with a specific User.

rails generate model User name:string email:string

Breakdown of the Model Generator

Rails Generate

rails generate model tells rails we want to generate a model called User.

The next parameters are the attributes of the model.

eg. rails generate User name:string email:string age:integer

Here is a list of some of the datatypes you can generate:

  • :primary_key
  • :string
  • :text
  • :integer
  • :float
  • :decimal
  • :datetime
  • :timestamp
  • :time
  • :date
  • :binary
  • :boolean
  • :references

Generator Files

Lets take a look at the files created by the generator.

Rails Generate

It created a User model file under app/models and a migration file under db/migrate.


Migrations describe changes to a database. It is describing the changes that are going to be made to the database.

Lets take a look at the migration file created by the rails generator.

Migration File

For the User migration file, we are creating a brand new table in our database called users with two fields, name a string and email also a string.

Running rails generate model will create both the model file and a migration file to accompany it.

Lets run our migrations with this command:

rails db:migrate

The Schema file

The schema.rb file is located in db folder and describes the schema of your database. All your migration changes should reflect here in one file.

This should be a one-to-one relation to the tables and columns in the database.

The Rails Console

The Rails Console enables us to inspect our rails app. Think of it as a sort of debugger for running our rails app.

We can run it by executing the following in terminal:

rails console

Lets start by creating a User object and saving it to the database.

Run the following in terminal under rails console:

user = User.new

Then run in the following commands for our example user in rails console:


user.name = "Eliel"


user.email = "eliel@test.com"



This will create a new User model and save it to the database

Inspecting our User objects from the database

We can query for objects in the rails console.

Lets fetch all users in our database.

users = User.all

This fetches all users in our database and puts them in a ruby array. So now users = [User]. We can print the first user in the array by running:

  1. first_user = users.first
  2. p first_user.name

Rails Console

Extra model enhancements

We have just completed creating our User model.

Lets modify it to make the email field unique, that way we cannot insert a User in to the database if there exists a User with the same email.

Adding a unique field to User

Exit your Rails Console, or open a new terminal. Run this in your terminal:

rails generate migration AddIndexToUsers

Open the file created by the rails generator. It should be under db/migrate and should end with addemailindextousers.

Paste the following into the change function.

add_index :users, :email, unique: true

Your migration file shoul look like this:

class AddEmailIndexToUsers < ActiveRecord::Migration[5.1]
  def change
    add_index :users, :email, unique: true

Now run:

rails db:migrate

Our User model is now unique to emails!


If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository or comment below.

Summer academy

An iOS Development Summer Course

Design, code and launch your own app. Locations in San Francisco and Asia

Find your location

Product College

A computer science college

Graduate into a successful career as a founder or software engineer.

Learn more

Cookies on Make School's website

We have placed cookies on your device to ensure that we give you the best experience on our website.

This site uses cookies to deliver our services. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Your use of Make School’s Products and Services is subject to these policies and terms.

Please note that Make School no longer supports Internet Explorer

We recommend upgrading to a modern web browser. Learn more