Ruby-on-rails 连接表的 Rails 命名约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11590469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:43:31  来源:igfitidea点击:

Rails naming convention for join table

ruby-on-railsruby-on-rails-3ruby-on-rails-3.1rails-migrations

提问by Yogzzz

This questions stems from: How to link form after creating rails join table

这个问题源于:How to link form after created 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?

连接表应该命名什么?category_products 或 category_products 或其他什么?

回答by Zabba

categories_products. Both plural. In lexical order.

categories_products. 都是复数。按词汇顺序。

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.

除非使用 :join_table 选项显式指定连接表的名称,否则 Active Record 会使用类名称的词法顺序创建名称。因此,客户和订单模型之间的连接将给出默认的连接表名称“customers_orders”,因为“c”在词法排序中的排名高于“o”。

回答by damoiser

Rails 4

导轨 4

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

请注意,从Rails 4 开始,有一些新规则。

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”.

指定与另一个类的多对多关系。这通过中间连接表关联两个类。除非连接表被明确指定为选项,否则使用类名的词法顺序来猜测它。因此,Developer 和 Project 之间的连接将给出默认的连接表名称“developers_projects”,因为“D”按字母顺序排在“P”之前。

请注意,此优先级是使用字符串的 < 运算符计算的。这意味着,如果字符串的长度不同,并且在与最短长度相比时字符串相等,那么较长的字符串被认为比较短的字符串具有更高的词汇优先级。例如,由于名称“paper_boxes”的长度,人们会期望表“paper_boxes”和“papers”生成“papers_paper_boxes”连接表名称,但实际上它生成“paper_boxes_papers”连接表名称。请注意此警告,并在需要时使用自定义 :join_table 选项。

如果您的表共享一个公共前缀,则它只会在开头出现一次。例如,表“catalog_categories”和“catalog_products”生成连接表名称“catalog_categories_products”。

=> Docs for Rails v4.2.7

=> 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

导轨 5

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

规则还是一样的。在Rails 5 中,我们有一个新的助手来创建带有迁移的连接表:

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

=> Edge docs for Rails

=> Rails 的 Edge 文档

回答by Prabhakar Undurthi

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

Rails 中的连接表只能按字母顺序创建。每次创建连接表时请记住这一点。

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

句法: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

示例 2:在 BlogPost 表和用户表之间创建连接表

BlogPost-User

blog_posts_users      # In alphabetical order with plural names

回答by Taylored Web Sites

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.

新的 create_join_table 迁移创建了一个没有对应模型的连接表,因此模型名称不需要命名约定。

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

要访问连接,您必须在两个表上声明 has_and_belongs_to_many,并通过创建的关联访问它们。