title: Rails - using uniqueness scope: on 2 columns at once
body: <p>I want to have uniqueness on 3 columns a.k.a. If the three columns are the same it isn't valid</p>
  
  <p>If the columns <code>value</code>, <code>categroy_item_id</code> and <code>key</code> are the same I want it to be invalid. But if one of the 3 columns is different and the other 2 are same then its fine.</p>
  
  <p>Here is what I tried</p>
  
  <pre><code>validates :value, length: { maximum: 65000 }, uniqueness: {  case_sensitive: false, scope: :category_item_id &amp;&amp; :key, if: 'key_type == 2'  }
  </code></pre>
  
  <p>With the above code if records have the same <code>key</code> the same <code>value</code> and different <code>category_item_id</code> it is counted as invalid (because of duplicate value) when it should be valid because the <code>category_item_id</code> is different. If it have the same <code>key</code> and <code>category_item_id</code> but a different <code>value</code> or same <code>category_item_id</code> and <code>value</code> with a different <code>key</code> it passes as valid. So its only the case of the same <code>key</code> the same <code>value</code> and different <code>category_item_id</code> they  it classes as invalid.</p>
  
  <p>I cant figure out if its the <code>&amp;&amp;</code> on the scope (I haven't found an official way to do it) or the coding is working but I'm not understanding it right and need to change something to get the result I want.</p>
  
  <p>(Everything has a <code>key_type</code> of <code>2</code> incase you noticed that at the end.)</p>