## Command Line Generator Info [Reference](https://stackoverflow.com/questions/12900170/is-there-a-reference-cheat-sheet-for-rails-generate-command) You can get all of this information on the command line. `rails generate` with no generator name will output a list of all available generators and some information about global options. `rails generate GENERATOR --help` will list the options that can be passed to the specified generator. ## Rails Generate Examples #### Create a Resource rails generate scaffold Post name:string title:string content:text #### Generate Models rails generate model Post title:string body:text published:boolean #### Add Column to Existing Model rails generate migration AddFieldToModel field:type #### Column Types :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean #### Adding a Unique Property to a Field rails generate scaffold Post name:string title:string content:text slug:string:uniq #### Adding a Unique Property to a Field rails generate scaffold Post name:string title:string content:text slug:string:uniq #### Adding Modifiers [Reference](http://www.chrisjmendez.com/2016/07/01/rails-cheatsheet/) Modifiers are inserted through curly braces and they allow you to include things such as null and limit. Add an age to a Friend with a limit rails g model friend age:integer{2} Add a price to a product with 2 decimals rails g model product 'price:decimal{10,2}' Would result in a migration with a `scale` of `2` and `percision` of `10` ``` class CreateProducts < ActiveRecord::Migration def change create_table :products do |t| ... t.decimal :price, precision: 10, scale: 2 ... t.timestamps null: false end end end ``` #### Create a new model with a reference to another model [Reference](https://www.ralfebert.de/snippets/ruby-rails/models-tables-migrations-cheat-sheet/) rails g model Supplier name:string rails g model Product name:string:index sku:string{10}:uniq count:integer description:text supplier:references popularity:float 'price:decimal{10,2}' available:boolean availableSince:datetime image:binary Resulting migrations: ``` class CreateSuppliers < ActiveRecord::Migration def change create_table :suppliers do |t| t.string :name t.timestamps null: false end end end class CreateProducts < ActiveRecord::Migration def change create_table :products do |t| t.string :name t.string :sku, limit: 10 t.integer :count t.text :description t.references :supplier, index: true, foreign_key: true t.float :popularity t.decimal :price, precision: 10, scale: 2 t.boolean :available t.datetime :availableSince t.binary :image t.timestamps null: false end add_index :products, :name add_index :products, :sku, unique: true end end ``` ## Rspec Generators [Documentation](https://relishapp.com/rspec/rspec-rails/docs/generators) rails generate rspec:model widget will create a new spec file in `spec/models/widget_spec.rb` The same generator pattern is available for all specs: ``` scaffold model controller helper view mailer observer integration feature job ``` Generating specific views ``` rails g rspec:view widget index edit new show create spec/views/widget create spec/views/widget/index.html.erb_spec.rb create spec/views/widget/edit.html.erb_spec.rb create spec/views/widget/new.html.erb_spec.rb create spec/views/widget/show.html.erb_spec.rb ```