Q:

Rails 4 has_many through for many to many association fails when creating association

Here is my schema:
create_table :policies do |t|
  t.string :name

  t.timestamps
end

create_table :permissions do |t|
  t.string :name
  t.string :method
  t.string :controller

  t.timestamps
end

create_join_table :policies, :permissions do |t|
  t.index :policy_id
  t.index :permission_id
end

And here is the code I am using to create the records and their associations:
policy = Policy.create! name: "View All"
permission = Permission.create!({
  name: "View Events",
  method: "index",
  controller: "Events"
})

policy.permissions << permission

And its returning the following error:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "permission_policies" does not exist

The table name that was created through the migration is policies_permissions
I wonder if this is an issue with the class names not being inferred properly

A:

Swap the positions of permissions and policies in the migration so that you create permissions_policies instead of policies_permissions. Rails infers the class names for a join table in alphabetical order.
See this answer for more information on join table naming conventions.

