问题
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