Ruby on Rails/ActiveRecord/Naming

Basics
ActiveRecord uses convention for naming classes, tables and fields. Rails uses convention over configuration. ActiveRecord expects applications to follow certain naming conventions. These conventions extend from file naming, class naming, table naming and more. By default classes are singular, tables are plural, primary keys are id and foreign keys are table_id.

Note: There are also certain names that are reserved and should not be used in your model for attributes outside of the conventions defined in Rails:


 * lock_version
 * type - This is only used when you have single table inheritance and must contain a class name
 * id - Reserved for primary keys
 * table_name_count - Reserved for counter cache
 * position - Reserved for acts_as_list
 * parent_id - Reserved for acts_as_tree
 * <tt>lft</tt> - Reserved for acts_as_nested_set
 * <tt>rgt</tt> - Reserved for acts_as_nested_set
 * <tt>quote</tt> - Method in ActiveRecord::Base which is used to quote SQL
 * <tt>template</tt>
 * <tt>class</tt>

Classes
ActiveRecord classes are named in singular form.

Tables
Tables for ActiveRecord objects are named in plural form by default. This pluralization is often an initial point of contention for new Rails users.

For a class named 'Dog', the default table name is 'Dogs'.

If you need to change the table name there are several ways to override the default behavior.

Set use_pluralization
In <tt>config/environment.rb</tt> you can specify <tt>ActiveRecord::Base.use_pluralization = false</tt>. This will apply to all ActiveRecord objects.

Use set_table_name
You can call <tt>set_table_name</tt> to specify a custom table name for a particular model.

For example:

Override table_name
You can also override the <tt>table_name</tt> method and return a value.

For example: