Ruby and Rails Naming Conventions

Here is a consolidated list of Naming Conventions in Ruby on Rails, that I have collected from the internet. If anything is missing from the list, please mention it in the comments section below :

Entity Description
Class and Module
  1. Classes and module names starts with an uppercase letter
  2. Multiple words are named using MixedCase
eg.: module Encryption, class MixedCase
Instance Methods
  1. Method name should start with a lowercase letter
  2. can have lower case letters, digits and underscores
eg.: paint(...), close_the_door(...)
Local Variables
  1. All Lowercase letters
  2. Use underscores instead of camelBack for multiple words
eg.: total, student_name
Instance Variables
  1. Begins with '@' sign and then followed by lowercase letters
  2. Use underscores for multiple words
eg.: @total, @student_name
Global Variables
  1. Starts with a dollar ($) sign followed by other characters
eg.: $global_variable
Class Variables
  1. Class variable names start with a double "at" sign (@@)
  2. may be followed by digits, underscores and lower case letters
eg.: @@color
  1. Constant names start with an uppercase letter followed by other characters.
  2. Constant objects are by convention named using all uppercase letters and underscores between words
  1. Follows the convention of Class names of unbroken MixedCase letters
  2. Always Singular format of the corresponding Table Name in database
  3. file name for the Models should always be in lowercase. For Multiple words use '_'
eg.: Order (table name: orders | file name: /app/models/order.rb )
  1. Singularized Table name with the suffix 'Controller'
  2. file name for the Controller should be in lowercase. Use '_' for multiple words format.
eg.: OrdersController (file: /app/controllers/order_controller.rb)
  1. File name should strictly have the correspodning action name from the controller
  2. File name should be in lowercase with underscore for multiple words format
eg.: app/views/order/index.html.erb
Files & Diretories
  1. Files and Directories are named using lowercase letters and '_' for multiple words formats
  2. Proper naming and pluralization is mandatory for the conventional functioning of Rails
eg.: app/helpers/orders_helper.rb, app/models/order.rb
Database Tables
  1. Table names have all lowercase letters and underscores between words, also all table names need to be plural
e.g. invoice_items, orders
Primary Key
  1. The primary key of a table is assumed to be named id.
Foreign Key
  1. 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
  1. Tables used to join two tables in a many to many relationship is named using the table names they link, with the table names in alphabetical order
eg.: items_orders
Timestamp Columns
  1. You can get ActiveRecord to automatically update the create and update times of records in a database table. To do this create two specially named columns created_at and updated_at to your table,
    i.e. t.datetime :created_at and t.datetime :updated_at.
  2. If you only want to store the date rather than a date and time, use :created_on and :updated_on.

No comments:

Post a Comment