Rails fixtures get table names wrong

耗尽温柔 提交于 2019-12-08 03:27:58

问题


I have a roles table, a permissions table, and to join them in a many-to-many relationship a roles_permissions table.

In the roles.yml file I have this:

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

In the permissions.yml I have:

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

Now rake test gives me errors saying no such table: permissions_roles: DELETE FROM "permissions_roles"

When I comment out the line in roles.yml that specifies the admin permissions then the errors go away.

It looks like the fixtures are assuming the joining table is named permissions_roles when it's actually named roles_permissions

How do I tell it the right table name?


回答1:


By convention, the join table name between two tables is named alphabetically.

So, the join table between roles and permissions should be named permissions_roles.




回答2:


You need to create the table permissions_roles instead of roles_permissions, or set the table in the options(: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

Read more here.



来源:https://stackoverflow.com/questions/31389375/rails-fixtures-get-table-names-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!