Rails naming convention for join table

2020-01-27 02:33发布

This questions stems from: 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?

4条回答
该账号已被封号
2楼-- · 2020-01-27 02:55

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
查看更多
虎瘦雄心在
3楼-- · 2020-01-27 03:02

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.

查看更多
三岁会撩人
4楼-- · 2020-01-27 03:03

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.

查看更多
forever°为你锁心
5楼-- · 2020-01-27 03:08

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

查看更多
登录 后发表回答