By default, Active Record uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class Book, you should have a database table called books. The Rails pluralization mechanisms are very powerful, being capable of pluralizing (and singularizing) both regular and irregular words. When using class names composed of two or more words, the model class name should follow the Ruby conventions, using the CamelCase form, while the table name must contain the words separated by underscores. Examples:
Model Class - Singular with the first letter of each word capitalized (e.g., BookClub).
Database Table - Plural with underscores separating words (e.g., book_clubs).
|Model / Class
|Table / Schema
Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns.
Foreign keys - These fields should be named following the pattern singularized_table_name_id (e.g., item_id, order_id). These are the fields that Active Record will look for when you create associations between your models.
Primary keys - By default, Active Record will use an integer column named id as the table’s primary key (bigint for PostgreSQL and MySQL, integer for SQLite). When using Active Record Migrations to create your tables, this column will be automatically created.
There are also some optional column names that will add additional features to Active Record instances:
created_at - Automatically gets set to the current date and time when the record is first created.
updated_at - Automatically gets set to the current date and time whenever the record is created or updated.
lock_version - Adds optimistic locking to a model.
type - Specifies that the model uses Single Table Inheritance.
(association_name)_type - Stores the type for polymorphic associations.
(table_name)_count - Used to cache the number of belonging objects on associations. For example, a comments_count column in an Article class that has many instances of Comment will cache the number of existent comments for each article.
There are also some optional column names that will add additional features to Active Record instances by other gems:
position - Represents the position of the record in a list. Use by
parent_id - Represents the parent id of the record. Use by
deleted_at - Automatically set to the current date and time whenever the record is deleted. Use by
There are also some optional tables names that will add additional features to Active Record by other gems:
- versions - Automatically insert to the an version record whenever the traget record is created, updated, or deleted. Use by