Rails generators CLI cheatsheet
Because I got tired of forgetting the syntax and looking it up all the time
1. Model Generators
To create a model, follow this formula:
rails g model NameOfModel column_name:datatype column_name2:datatype2 --no-test-framework
For example:
rails g model Author name:string genre:string bio:text --no-test-framework
will generate this file:
# db/migrate/20180701032833_create_authors.rbclass CreateAuthors < ActiveRecord::Migration[5.1]
def change
create_table :authors do |t|
t.string :name
t.string :genre
t.text :bio t.timestamps
end
end
end
2. Migration Generators
Adding Columns
To add an additional column to a table, follow this formula:
rails g migration add_name_of_column_to_table_name name_of_column:datatype --no-test-framework
For exmple, if we want to add the year an author debuted his/her first book, we would type in the following:
rails g migration add_debut_year_to_authors debut_year:integer
…