Basic Guide to Rail Generators

Photo by American Public Power Association on Unsplash

Imagine be able to type a line of code in your terminal that generates files for you.

Generator - a command line shortcut that creates and edits files using boilerplate code

Welcome to the world of rail generators.

If you’re familiar with Rails, then you’ve probably come across some kind of generator.

If you’re using generators as a crutch, then I would go back to the drawing board and learn the required file types first before relying on generators.

How to access rails generators?

Photo by Matt Duncan on Unsplash

The beauty of working with Ruby on Rails is that it gives you a set of pre-installed generators to work with.

rails g

This will give you a list of generators that are available for you. Once you have a better understanding of generators, you will have the ability to create a custom generator.

Let’s walk through some of the most used generators and discuss the pro’s and cons of using them.

1. Scaffold Generator

Wanting to get your application up and running as quickly as possible? The scaffold generator is your friend.

rails generate scaffold Resource column_name:datatype

Utilizing the scaffold template above will create a complete model, database migration for that model, controller to modify it, view pages, and a test suite.

Photo by Viktor Talashuk on Unsplash

Most Rails developers avoid using the scaffold generator because it creates a ton of unnecessary files. It can actually be more time consuming to go through the excess files rather than creating them from scratch.

2. Model Generators

Looking to create a model without having to create it from scratch? Simply use the template below.

rails g model NameOfModel column_name:datatype

For example if you’re using the code below.

rails g model Drivers name:string age:integer location:string

Entering this command line will generate this file:

# db/migrate/20180701032444_create_drivers.rbclass CreateDrivers < ActiveRecord::Migration[5.1]  def change
create_table :drivers do |t|
t.string :name
t.integer :age
t.string :location
end
end
end

Pros: Saves time, standard necessary files.

Con: If you do not include “ — no-test-framework” then it’ll create unnecessary test files.

3. Migration Generators

Photo by Jan-Niclas Aberle on Unsplash

Migration generators doesn’t really have any cons. You can save time adding and removing columns by using the command line models below.

Adding Columns

You forgot a column in one of your data tables? That’s okay. Simply use the generator below!

rails g migration add_column_name_to_table_name name_of_column:datatype

For example if you’re adding a column to the table above:

rails g migration add_birth_year_to_drivers birth_year:integer

Will create..

# db/migrate/20180701032326_add_birth_year_to_drivers.rbclass AddBirthYearToDrivers < ActiveRecord::Migration[5.1]
def change
add_column :drivers, :birth_year, :integer
end
end

Tip: Don’t forget to add “ — no-test-framework” to avoid test files being created.

Removing Columns

Maybe you were a little column happy when you created your model and need to remove some. Use the template below.

rails g migration remove_column_name_from_table_name column_name:datatype

To continue with our example above, you will write something like this.

rails g migration remove_location__from_drivers location:string --no-test-framework

Creates something like this..

#db/migrate/20180701122326_remove_location_from_drivers.rbclass RemoveLocationFromDrivers < ActiveRecord::Migration[5.1]
def change
remove_column :drivers, :location, :string
end
end

Don’t forget to rake db:migrate to initiate the new changes. If you forget this step, then your schema will not be updated!

4. Resource Generators

Using a resource generator will create a new Model, a database table that corresponds, controller, and an empty views folder type in.

rails g resource ModelName column_name:datatype

If I want to create a Routes model with a corresponding trip that a route will belong to:

rails g resource Route destination:string trip_id: integer

Create Your OWN!

Photo by Sigmund on Unsplash

Can’t find a generator for your liking? Rails gives you the ability to create your own generator with a generator creator.

Here is a great article that guides you to creating your very own Rails generator.

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