Rails 个灯具的 table 个名字有误

Rails fixtures get table names wrong

我有一个rolestable,一个permissionstable,要加入他们的多对多关系roles_permissionstable.

roles.yml 文件中我有这个:

admin:
  name: admin
  permissions: create_user, edit_user, view_all_users

permissions.yml 我有:

create_user:
  name: create_user
  description: Create a new user

edit_user:
  name: edit_user
  description: Edit any user details

view_all_users:
  name: view_all_users
  description: View all users

现在 rake test 给我错误说 no such table: permissions_roles: DELETE FROM "permissions_roles"

当我注释掉 roles.yml 中指定管理员权限的行时,错误就会消失。

看起来固定装置假设连接 table 被命名为 permissions_roles 而实际上它被命名为 roles_permissions

如何告诉它正确的 table 名称?

按照惯例,两个 table 之间的连接 table 名称按字母顺序命名。

因此,rolespermissions 之间的联接 table 应命名为 permissions_roles

您需要创建 table permissions_roles 而不是 roles_permissions,或者在选项中设置 table( :join_table):

has_and_belongs_to_many :roles, join_table: "roles_permissions"

#has_and_belongs_to_many:

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

阅读更多here