title: Rails has_many but unique on associated model's column
body: <p>I have a User model:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :products
  end
  </code></pre>
  
  <p>and a Product model:</p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
      belongs_to :user
  end
  </code></pre>
  
  <p>The Product table has a column named <code>product_id</code> which is alpha-numeric and I want that a user can have many products but should be unique on <code>product_id</code>. I can add an index on <code>product_id</code> to be <code>unique</code> but that would mean another user can't have a similar product.</p>
  
  <p><strong>To simplify the problem, there should be a unique entry per</strong> <code>user_id</code> <strong>and</strong> <code>product_id</code><strong>.</strong></p>
  
  <p>Is there any conventional method to achieve the same in Rails ?</p>