- Class names: Use PascalCase for class names:
class ThisIsTheNameOfAClass
end
class Profile
end- Methods and Variables names: Use snake_case for both
## Method:
def example_method
end
## Variable:
awesome_variable = "Hello RoR!"- There are some special methods
## Methods with a `?` suffix will return a boolean.
def admin?
role == 'admin'
end
user = User.new(role: 'admin')
user.admin? # => true
## 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.
user = User.new(email: nil)
user.save # => false
user.save! # => ActiveRecord::RecordInvalid: Validation failed: Email can't be blankDatabase tables use snake_case. Table names are plural.
Column names in the database use snake_case, but are generally singular.
Example:
+------------------------------+
| profiles |
+---------------------+--------+
| id | ID |
| name | STRING |
| years_of_experience | INT |
| birthdate | DATE |
+---------------------+--------+
Model class names use PascalCase. 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:
# app/models/profile.rb
class Profile < ActiveRecord::Base
# Methods follow the same conventions as attributes
def instructor?
years_of_experience > 2
end
endRelations use snake_case and follow the type of relation, so has_one and belongs_to are singular while has_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:
# app/models/article.rb
class Article < ActiveRecord::Base
# This knows to use the profile_id field in the database
belongs_to :profile
end# app/models/profile.rb
class Profile < ActiveRecord::Base
# This knows to look at the Article class and find the foreign key in that table
has_many :articles
endController class names use PascalCase and have Controller as a suffix. The Controller 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:
# app/controllers/article_controller.rb
ArticleController < ApplicationController
def index
# ...
end
def show
# ...
end
# etc
end# app/controllers/profiles_controller.rb
ProfilesController < ApplicationController
def show
# ...
end
# etc
endRoute names are snake_case, and usually match the controller. Most of the time routes are plural and use the plural resources.
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:
resources :articles
# Users can only see their own profiles, so we'll use `/profile` instead
# of putting an id in the URL.
resource :profileView 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:
app/views/article/index.html.erbapp/views/article/show.html.erbapp/views/profile/show.html.erb