title: How to implement composite primary keys in rails
body: <p>I have a <code>User</code> model that looks like this:</p>
  
  <pre><code>class User &lt; ApplicationRecord
    belongs_to :organization
    belongs_to :department
  end
  </code></pre>
  
  <p>The <code>users</code> table in the database has the two foreign keys <code>organization_id</code> and <code>department_id</code>. How can I make these two columns a composite primary key? I've seen two approaches online so far:</p>
  
  <h2>Option 1</h2>
  
  <p>Use the <code>composite_primary_keys</code> gem</p>
  
  <h2>Option 2</h2>
  
  <p>Add an index to each of the two columns using something like this:</p>
  
  <pre><code>add_index :users, [:organization_id, :department_id], unique: true
  </code></pre>
  
  <h2>My question</h2>
  
  <p>What is the best way to uniquely identify a row in the <code>users</code> table where a row must have a <code>department_id</code> and an <code>organization_id</code> in order to be uniquely identified? What's the difference between indexing both columns and simply making each column a primary key of the table? </p>
  
  <p>Thanks!</p>