Search code examples
ruby-on-railsruby-on-rails-4fixtures

Rails fixtures get table names wrong


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?


Solution

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