Oct 28
Ruby and Rails Naming Conventions
I’ve been looking for a consolidated list of all Ruby and Rails naming conventions without too much luck so I’ve started my own. I find I always forget the naming convention especially as I move between projects that use different languages.
Please let me know of any others that I have missed.
Naming Conventions
Ruby Naming Convention
Ruby uses the first character of the name to help it determine it’s intended use.
Local Variables
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_xyzInstance Variables
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. @colourInstance Methods
Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters, e.g. paint, close_the_doorClass Variables
Class variable names start with a double "at" sign (@@) and may be followed by digits, underscores, and letters, e.g. @@colourConstant
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_CONSTANTClass and Module
Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCaseGlobal Variables
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:
Variable
Variables are named where all letters are lowercase and words are separated by underscores, e.g. order_amount, totalClass and Module
Classes and modules use MixedCase and have no underscores, each word starts with a uppercase letter, e.g. InvoiceItemDatabase Table
Table names have all lowercase letters and underscores between words, also all table names need to be plural, e.g. invoice_items, ordersModel
The 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
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
Primary Key
The primary key of a table is assumed to be named id.Foreign Key
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
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, for example items_orders.Automated Record Timestamps
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. If you only want to store the date rather than a date and time, use :created_on and :updated_on.
Naming Convention Summary
Model Naming Convention
Table: orders
Class: Order
File: /app/models/order.rb
Primary Key: id
Foreign Key: customer_id
Link Tables: items_ordersController Naming Convention
Class: OrdersController
File: /app/controllers/orders_controller.rb
Layout: /app/layouts/orders.html.erbView Naming Convention
Helper: /app/helpers/orders_helper.rb
Helper Module: OrdersHelper
Views: /app/views/orders/… (list.html.erb for example)Tests Naming Convention
Unit: /test/unit/order_test.rb
Functional: /test/functional/orders_controller_test.rb
Fixtures: /test/fixtures/orders.yml


September 17th, 2007 at 12:26 pm
Thanx a million I am a newbie to RoR, and some tutorials dont really make it clear that you have to stick to the naming concentions to get on the Wagon.
Thanks Again!
September 17th, 2007 at 12:27 pm
Thanks a million I am a newbie to RoR, and some tutorials dont really make it clear that you have to stick to the naming conventions to get on the Wagon.
Thanks Again!
September 17th, 2007 at 12:54 pm
No problem at all, I had the same issue. I could not find a list of conventions anywhere telling me what I needed to do, very frustrating. If you come across any other conventions please let me and I’ll add it to the list.
November 9th, 2007 at 7:44 am
[…] Ruby and Rails Naming Conventions […]
November 12th, 2007 at 12:45 pm
[…] Ruby and Rails Naming Conventions […]
April 15th, 2008 at 11:30 pm
[…] in the case of Ruby On Rails, which is of heavily opinionated nature, there are many conventions that can be broken by mistake which may introduce errors that do no show up initially. This […]
April 18th, 2008 at 1:12 am
One more that doesn’t affect many things, but still:
? after a method call means it returns a boolean and has NO side effects
! after a method call means it modifies the object it is called on OR the parameters passed in. This seems to be inconsistent at best.
April 30th, 2008 at 12:54 pm
[…] There is more information about Rails and Ruby naming conventions over here. […]
May 7th, 2008 at 2:22 am
I haven’t been able to find an answer on a particular convention for PKs/FKs. I have an identifiers table with various fields, a users table and lets say a profiles table. The users table has a primary key of identifier_id (i.e., the PK is a FK). This works perfectly fine; however, I do not know how to extend this further down. In the profiles table I need to reference the users table, but do I use user_id or something different? I have not found documentation for this and haven’t started developing this app yet (I want to get the DB set first).
May 7th, 2008 at 8:20 am
Hi Jeffrey,
If I understand you correctly you could setup your structure like this:
identifiers
id
|
|
users
id
identifier_id
|
|
profiles
id
user_id
You are able to access the user of the profile by using the user_id, you can then find the identifier by using the identifier_id in the users table.
November 13th, 2008 at 8:18 am
Thank you very much for the concise summary.
November 26th, 2008 at 8:34 am
Okay, this is more of a practical question, but I want to create a “blog” in rails. I however, do not want the controller to be called “Blogs”, and I don’t want the table to be called “blogs”.
The best thing I can think of is to call it something like “articles”, however, this breaks other things, like the current_page? (or whatever) method.
Any feedback is appreciated!
February 6th, 2009 at 1:21 am
What if I want to organize some models in a module? E.g. what should I call the table corresponding to Admin::Group ? “Admin::Group”.tableize => “admin/groups” which is not acceptable table name. Just using groups could cause name clash.
February 11th, 2009 at 8:04 am
Hi Etienne,
Many thanks for the post.
I have a question; I need to create a join table and the convention is to use the other two table names in alphabetic order to name this join table. One of my tables is called “words” and the other one is called “word_types2. My question is: what is the alphabetic order here? Does “s” in words come before “_” in word_types or is it the other way around? My guess is that the first is true and therefore the join table should be called: words_word_types.
Many thanks!!
February 11th, 2009 at 8:06 am
Sorry, there is a typo in my previous comment; the second table name should read “word_types”, not “word_type2.
Regards.
February 11th, 2009 at 9:02 am
Hi Joseph,
That is a good question, I like you am not sure about the answer in this instance but I think I’d agree words_word_types would be the way to go.
February 15th, 2009 at 7:57 pm
Hi Etienne,
Thanks, I’ll dig around a bit and will post the answer here when I find out.
Cheers
February 16th, 2009 at 2:33 am
Got it!
I found here: http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000430 that:
The lexical order: “is calculated using the
February 16th, 2009 at 2:35 am
Sorry, part of the post got cut off. I’ll try again..
The lexical order: “is calculated using the < operator for String. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one.”
What that basically means is that, contrary to what we thought, word_types comes before words, because it is longer, and therefore the join table should be named “word_types_words”.
I am still not quite sure, though, whether the underscore (_) would come before or after alphabetic characters, but for now my query is answered.
May 15th, 2009 at 5:30 am
Just a note to say thank you for compiling this helpful list.
All the best,
Andrew
May 15th, 2009 at 10:39 am
Hi Andrew,
Thank you for the feedback, I’m glad you found it helpful.
Cheers,
Etienne.
June 6th, 2009 at 12:30 am
Also, Class and Module names cannot contain “.”
June 10th, 2009 at 2:42 am
What about methods that end in an exclamation point? I think that there is a difference between Ruby and Rails conventions here. I believe that in Ruby, methods appended with ! modify the object they are working with instead of returning a new object, whereas in Rails, these methods will throw an exception if the operation fails.
For example:
Ruby, Array.collect vs Array.collect!
Rails ActiveRecord::Base.save vs ActiveRecord::Base.save!
Thanks,
Janz
May 11th, 2010 at 2:31 am
“If the model class name has multiple capitalised words, the table name is assumed to have underscores between these words” …. as is the class definition filename
July 24th, 2010 at 11:16 pm
as i know, the tables should have “s”, and the models should not, but what if my table name consists of two words like “contribution_status” (note:::: it is not a relation or a link table) is it correct to be named “contributions_statuses” as a table, and “Contribution_status” as a model ?