RoR - Convention Over Configuration

Variables

CategoryExample
Local Variablesvariable_xyz
Instance Variables@foo
Class Variables@@foo
ConstantTHIS_IS_A_CONSTANT
Global Variables$global

Models & Controllers & DB Tables

CategorySpecificationExamples
ModelsMixedCase; Singularclass Post
ControllersMixedCase; Pluralizationclass PostsController
Primary KeyAssumed to be named idid
Foreign KeyAssumed to be named XXX_idauthor_id
Join TablesXXX_YYY with the table names in alphabetical orderitems_orders

As for the normal tables, we usually use pluralization of the corresponding model name:

Model / ClassTable / Schema
Articlearticles
LineItemline_items
Deerdeers
Mousemice
Personpeople

Migrations

When generating a migration in RoR, we can use the commands look like this:

rails generate migration CreatePosts title:string content:text

And db/migrate/YYYYMMDDHHMMSS_create_posts.rb file would be generated:

class CreatePosts < ActiveRecord::Migration[{Version-Number}]
  def change
    create_table :posts do |t|
      t.string :title
      t.text    :content
    end
  end
end

Because of the naming convention of RoR, if you name the migration Create${Models}, the table called ${Models} would be generated automatically, we have more of these conventions in RoR migration:

ConventionMeaning
AddXXXToYYYA migration containing the appropriate add_column would be generated
RemoveXXXFromYYYA migration containing the appropriate remove_column would be generated
CreateJoinTableXXXYYYA migration containing create_join_table of XXX and YYY would be generated
· Ruby on Rails, Web