# Rails generate commands
rails generate GENERATOR_NAME [args] [options].
rails generate to list available generators. Alias:
# Rails Generate Model
To generate an
ActiveRecord model that automagically creates the correct db migrations & boilerplate test files for your model, enter this command
rails generate model NAME column_name:column_type
'NAME' is the name of the model. 'field' is the name of the column in the DB table and 'type' is the column type (e.g.
body:text). Check the Remarks section for a list of supported column types.
To setup foreign keys, add
So say you wanted to setup a
User model that has a
School, you would type in the following
rails generate model User username:string email:string school:belongs_to
rails g is shorthand for
rails generate. This would produce the same result
rails g model User username:string email:string school:belongs_to
# Rails Generate Migration
You can generate a rails migration file from the terminal using the following command:
rails generate migration NAME [field[:type][:index] field[:type][:index]] [options]
For a list of all the options supported by the command, you could run the command without any arguments as in
rails generate migration.
For example, if you want to add
last_name fields to
users table, you can do:
rails generate migration AddNamesToUsers last_name:string first_name:string
Rails will create the following migration file:
class AddNamesToUsers < ActiveRecord::Migration[5.0] def change add_column :users, :last_name, :string add_column :users, :first_name, :string end end
Now, apply the pending migrations to the database by running the following in the terminal:
Note: For even less typing, you can replace
# Rails Generate Scaffold
DISCLAIMER: Scaffolding is not recommended unless it's for very conventional CRUD apps/testing. This may generate a lot of files(views/models/controllers) that are not needed in your web application thus causing headaches(bad 😦).
To generate a fully working scaffold for a new object, including model, controller, views, assets, and tests, use the
rails g scaffold command.
Then you can run
rake db:migrate to set up the database table.
Then you can visit http://localhost:3000/widgets (opens new window) and you'll see a fully functional CRUD scaffold.
# Rails Generate Controller
we can create a new controller with
rails g controller command.
$ bin/rails generate controller controller_name
The controller generator is expecting parameters in the form of
generate controller ControllerName action1 action2.
The following creates a Greetings controller with an action of hello.
$ bin/rails generate controller Greetings hello
You will see the following output
This generates the following
|Functional Test File|
It will also add routes for each action in
| ||Get help on any generator command|
| ||Pretend Mode: Run generator but will not create or change any files|
| ||'field-name' is the name of the column to be created and 'type' is the data-type of column. The possible values for 'type' in |
The possible values for 'type' in
| ||For smaller pieces of text (usually has a character limit of 255)|
| ||For longer pieces of text, like a paragraph|
| ||Storing data including images, audios and videos|
| ||Storing true or false values|
| ||Only the date|
| ||Only the time|
| ||Date and time|
| ||Storing floats without precision|
| ||Storing floats with precision|
| ||Storing whole numbers|