Naming Conventions
General Ruby conventions
Class names are
CamelCase
.
Methods and variables are
snake_case
.
Methods with a
?
suffix will return a boolean.
Methods with a
!
suffix mean one of two things: either the method operates destructively in some fashion, or it will raise and exception instead of failing (such as Rails models'#save!
vs.#save
).
In documentation,
::method_name
denotes a class method, while#method_name
denotes a instance method.
Database
Database tables use
snake_case
. Table names are plural.
Column names in the database use
snake_case
, but are generally singular.
Example:
Model
Model class names use
CamelCase
. These are singular, and will map automatically to the plural database table name.
Model attributes and methods use
snake_case
and match the column names in the database.
Model files go in
app/models/#{singular_model_name}.rb
.
Example:
Relations in models
Relations use
snake_case
and follow the type of relation, sohas_one
andbelongs_to
are singular whilehas_many
is plural.
Rails expects foreign keys in the database to have an
_id
suffix, and will map relations to those keys automatically if the names line up.
Example:
Controllers
Controller class names use
CamelCase
and haveController
as a suffix. TheController
suffix is always singular. The name of the resource is usually plural.
Controller actions use
snake_case
and usually match the standard route names Rails defines (index
,show
,new
,create
,edit
,update
,delete
).
Controller files go in
app/controllers/#{resource_name}_controller.rb
.
Example:
Routes
Route names are
snake_case
, and usually match the controller. Most of the time routes are plural and use the pluralresources
.
Singular routes are a special case. These use the singular
resource
and a singular resource name. However, they still map to a plural controller by default!
Example:
Views
View file names, by default, match the controller and action that they are tied to.
Views go in
app/views/#{resource_name}/#{action_name}.html.erb
.
Examples:
Last updated