Unless you regularly work with Rails, it's inevitable that you'll end up forgetting the various commands aimed at making your life easy. Although Googling your questions is always a good approach to solving problems, I find this cheatsheet (and boilerplate app) to be slightly more helpful.

Basic Command Line

Rails generate is a command line tool that can help you quickly build Models, Controllers, tests, etc for your app.

Here's how you get quickly get an inventory of everything Rails g can do.

rails g -h
Rails:
  assets
  channel
  controller
  generator
  helper
  integration_test
  jbuilder
  job
  mailer
  migration
  model
  resource
  scaffold
  scaffold_controller
  task

Source

Common Commands

Create a Model and name it something singular.

rails g model my_model

Create a Controller and take notice of the pluralization.

rails g controller my_controllers

Update your database schema

rails db:migrate

Create a task

rails g task my_task action_one action_two

Advanced Command Line

Once you get the hang of using rails g, the next step is to start decorating it with data types, modifiers, etc.

Adding Datatypes

Datatypes help provide context about the Models you are making by describing them as Floating Point Numbers, Strings, Text, Integers, etc. By including a :datatype to your model, Rails will set up the database column to perfectly handle the object.

List of Data Types You Can Use

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

Create a Unique user with an index

rails g model user user_name:string:uniq

Adding Modifiers

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}'

Source


Tokens

Rails 5 now offers a way to provide token-based API access through the has_secure_token method.

Create a unique auth_token that can also be indexed.

rails g model User full_name:string password_digest:string auth_token:token password_reset_token:token description:text

Adding a Unique Token using rails g migration

rails g migration add_another_token_to_users another_token:token:unique

Append a token to an existing model.

rails g migration add_token_to_users my_token:token

Regenerating a token

You can use the regenerate_[name_of_token] pattern to create a new token. For example, in the rails migration above, here's an example of issuing a new password_reset_token

user = User.new
user.save
user.password_reset_token
user.regenerate_password_reset_token

Polymorphism

Suppose your building a collaborative app (like Pivotal Tracker and you want to add comments to projects, tasks, and attachments. You can do that by making comments polymorphic.

rails g model Comment body:text commentable:references{polymorphic}:index

Source

Single Table Inheritance

This will create a single inheritence.

rails g model admin --parent user

Source


Undo a rails generate

Undo a scaffold

rails d scaffold my_scaffold

Undo a model

rails d model my_model

Undo a controller

rails d controller my_controller

Views

Adding views to an existing model (Method #1)

Run a scaffold but skip the migration and don't overwrite the previous model.

rails g scaffold User --migration=false --skip

Adding views to an existing model (Method #2)

Use the erb templating engine and explicitly announce the fields.

rails g erb:scaffold User firstname lastname reputation

Source


Serializer

Scaffold works pretty much like always but if you decide to use the --api edition of rails, you will get a serializer instead of views.

Create a scaffold after a model has been created

rails g scaffold User --migration=false --skip

Authentication

When you're building an API-only app, it's unusual to use cookies, sessions and a heap of other browser-centric tools.

Instead, you'll want to use Javascript Web Tokens (JWT) and a token-based gem package like knock or devise.


Devise

Validate e-mail

validates_format_of :email,:with => Devise::email_regexp

Source