Show Menu
Cheatography

Ruby on Rails Cheat Sheet (DRAFT) by

Learn to program in Rails

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Migration Commands

bin/rails db:migrate
Update the database
bin/rails db:rol­lback
Undo the previous migration
bin/rails db:mig­rat­e:redo
First rollback, then migrate

Data Types

:bigint
really big number
:binary
alternate values
:boolean
true or false values
:integer
a number
 

Naming Conven­tions

CreateXXX col:type col:type
Creates a table named XXX with provided columns
AddXXX­ToYYY col:type
Adds column XXX to table YYY
Remove­XXX­FromYYY col:type
Removes column XXX from table YYY

Create Table

The
create­_table
method creates a new table.

Available Options:
-
primar­y_key: :key
: Customizes the primary key.
-
force: true
: Drop the table if it already exists. Warning: This will result in unwanted data loss, if the existing table is populated.
-
if_not­_ex­ists: true
: If you try to create a table with duplicate name, Rails will throw an error. This option lets you return silently without raising an error, if the table already exists.
-
options
: Provide databa­se-­spe­cific options.
-
temporary: true
: Create a temporary table that will only exist during the current connection to the database.
-
id: false
: Do not generate a primary key at all. Useful when creating join tables. However, as we'll see, better solutions exist for this, such as
create­_jo­in_­table
method.