Rails naming convention for join table Rails naming convention for join table ruby-on-rails ruby-on-rails

Rails naming convention for join table


categories_products. Both plural. In lexical order.

Quote:

Unless the name of the join table is explicitly specified by using the :join_table option, Active Record creates the name by using the lexical order of the class names. So a join between customer and order models will give the default join table name of “customers_orders” because “c” outranks “o” in lexical ordering.


Rails 4

Pay attention that from Rails 4 there are some new rules.

Specifies a many-to-many relationship with another class. This associates two classes via an intermediate join table. Unless the join table is explicitly specified as an option, it is guessed using the lexical order of the class names. So a join between Developer and Project will give the default join table name of “developers_projects” because “D” precedes “P” alphabetically.

Note that this precedence 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. For example, one would expect the tables “paper_boxes” and “papers” to generate a join table name of “papers_paper_boxes” because of the length of the name “paper_boxes”, but it in fact generates a join table name of “paper_boxes_papers”. Be aware of this caveat, and use the custom :join_table option if you need to.

If your tables share a common prefix, it will only appear once at the beginning. For example, the tables “catalog_categories” and “catalog_products” generate a join table name of “catalog_categories_products”.

=> Docs for Rails v4.2.7

# alphabetically orderdevelopers + projects                 -->  developers_projects # precedence is calculated with '<', lengthier strings have precedence # if the string are equal compared to the shortest lengthpaper_boxes + papers                  -->  paper_boxes_papers  # common prefix omittedcatalog_categories + catalog_products -->  catalog_categories_products 

Rails 5

The rule are still pretty the same. With Rails 5 we have a new helper for creating join tables with migrations:

class CreateDevelopersProjectsJoinTable < ActiveRecord::Migration[5.0]  def change    create_join_table :developers, :projects  endend

=> Edge docs for Rails


Join tables in Rails must be created in alphabetical order only. Keep this point in mind every time you create a join table.

For example, if you want to create a join table between project table and collaborator table you must name it like below.

Syntax: first_table_name(UNDERSCORE)second_table_name

# Names must be in alphabetical order and also in plural# Decide which is your first table name based on the alphabetical order

Example: Creating Join Table between Project And Collaborator

Collaborator-Projectcollaborators_projects   # you should name it  like this; In alphabetical order with plural names

Example 2: Creating Join Table between BlogPost table and User Table

BlogPost-Userblog_posts_users      # In alphabetical order with plural names