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 tables use snake_case. Table names are plural.
Column names in the database use snake_case, but are generally singular.
Example:
+--------------------------+
| bigfoot_sightings |
+------------+-------------+
| id | ID |
| sighted_at | DATETIME |
| location | STRING |
| profile_id | FOREIGN KEY |
+------------+-------------+
+------------------------------+
| profiles |
+---------------------+--------+
| id | ID |
| name | STRING |
| years_of_experience | INT |
+---------------------+--------+
rails g scaffold Part user:references- It adds a user_id column and also a belongs_to in the model.
- If you simply use: rails g scaffold Part user_id:integer ----> then belongs_to will be omitted from the model and you'll have to add that yourself.
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:
# app/models/bigfoot_sighting.rb
class BigfootSighting < ActiveRecord::Base
# This class will have these attributes: id, sighted_at, location
end# app/models/profile.rb
class Profile < ActiveRecord::Base
# Methods follow the same conventions as attributes
def veteran_hunter?
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/bigfoot_sighting.rb
class BigfootSighting < 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 BigfootSighting class and find the foreign key in that table
has_many :bigfoot_sightings
endController class names use CamelCase 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/bigfoot_sightings_controller.rb
BigfootSightingsController < 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 :bigfoot_sightings
# 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/bigfoot_sightings/index.html.erbapp/views/bigfoot_sightings/show.html.erbapp/views/profile/show.html.erb
rails generate migration add_fieldname_to_tablename fieldname:string
or
rails generate migration AddPartNumberToProducts
Please remember that the table name is pluralizedThis creates a migration based on the above settings- If you use the above conventions then you basically minimise the editing associated with your migration. You may though, be required to define and up and down methods in your migration file.
generate migration RemovePartNumberFromProducts part_number:string
- is the rails convention for removing part_number from the Products table.
rails g migration change_date_format_in_my_table
- then change the actual migration to reflect the new column type
class ChangeDateFormatInMyTable < ActiveRecord::Migration
def up
change_column :my_table, :my_column, :datetime
end
def down
change_column :my_table, :my_column, :date
end
end