Ruby and Rails Naming Conventions
Ruby Naming Convention
Ruby uses the first character of the name to help it determine it’s intended use.
Lowercase letter followed by other characters, naming convention states that it is better to use underscores rather than camelBack for multiple word names, e. g. mileage, variable_xyz
Instance variables are defined using the single “at” sign (@) followed by a name. It is suggested that a lowercase letter should be used after the @, e. g. @colour
Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters, e. g. paint, close_the_door
Class variable names start with a double “at” sign (@@) and may be followed by digits, underscores, and letters, e. g. @@colour
Constant names start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e. g. THIS_IS_A_CONSTANT
Class and Module
Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e. g. module Encryption, class MixedCase
Starts with a dollar ($) sign followed by other characters, e. g. $global
Rails Naming Convention
Rails use the same naming convention as Ruby with some additions:
Variables are named where all letters are lowercase and words are separated by underscores, e. g. order_amount, total
Class and Module
Classes and modules use MixedCase and have no underscores, each word starts with a uppercase letter, e. g. InvoiceItem
Table names have all lowercase letters and underscores between words, also all table names need to be plural, e. g. invoice_items, orders
model is named using the class naming convention of unbroken MixedCase and is always the singular of the table name, e. g. table name might be orders, the model name would be Order. Rails will then look for the class definition in a file called order. rb in the /app/models directory. If the model class name has multiple capitalised words, the table name is assumed to have underscores between these words.
Controller class names are pluralized, such that OrdersController would be the controller class for the orders table. Rails will then look for the class definition in a file called orders_controller. rb in the /app/controllers directory.
Files, Directories and other pluralization
Files are named using lowercase and underscores. Assuming we have an Orders controller then the following other conventions will apply:
That there is a helper module named OrdersHelper in the orders_helper. rb found in the app/helpers directory
Rails will look for view template files for the controller in the app/views/orders directory
Output from this view will then be used in the layout defined in the orders. html. erb in the app/views/layouts directory
Test files including order_test. rb will be created in the /test/unit directory, a file will be created in the /test/fixtures directory called orders. yml and finally a file called orders_controller_test. rb will be created in the /test/functional directory
The primary key of a table is assumed to be named id.
The foreign key is named with the singular version of the target table name with _id appended to it, e. g. order_id in the items table where we have items linked to the orders table.
Many to Many Link Tables