Skip to content

Instantly share code, notes, and snippets.

@tibbon
Created July 9, 2013 18:27
Show Gist options
  • Save tibbon/5959868 to your computer and use it in GitHub Desktop.
Save tibbon/5959868 to your computer and use it in GitHub Desktop.
Join tables without IDs

These need to be backed up by a migration to create the assemblies_parts table. This table should be created without a primary key:

class CreateAssembliesPartsJoinTable < ActiveRecord::Migration
  def change
    create_table :assemblies_parts, id: false do |t|
      t.integer :assembly_id
      t.integer :part_id
    end
  end
end

We pass id: false to create_table because that table does not represent a model. That's required for the association to work properly. If you observe any strange behavior in a has_and_belongs_to_many association like mangled models IDs, or exceptions about conflicting IDs, chances are you forgot that bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment