Rails naming convention for join table

Ruby on-RailsRuby on-Rails-3Ruby on-Rails-3.1Rails Migrations

Ruby on-Rails Problem Overview


This questions stems from: https://stackoverflow.com/questions/11313872/how-to-link-form-after-creating-rails-join-table

I am creating the join table between my Product and Category Models.

What should the join table be named? categories_products or category_products or something else?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

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 order
developers + projects                 -->  developers_projects 

# precedence is calculated with '<', lengthier strings have precedence 
# if the string are equal compared to the shortest length
paper_boxes + papers                  -->  paper_boxes_papers  

# common prefix omitted
catalog_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
  end
end

=> Edge docs for Rails

Solution 3 - Ruby on-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-Project

collaborators_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-User
   
blog_posts_users      # In alphabetical order with plural names

Solution 4 - Ruby on-Rails

The new create_join_table migration creates a join table that has no corresponding model, so no naming convention is required for the model name.

To access the join, you must declare has_and_belongs_to_many on the two tables, and access them through the association created.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYogzzzView Question on Stackoverflow
Solution 1 - Ruby on-RailsZabbaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsdamoiserView Answer on Stackoverflow
Solution 3 - Ruby on-RailsPrabhakar UndurthiView Answer on Stackoverflow
Solution 4 - Ruby on-RailsTaylored Web SitesView Answer on Stackoverflow