ruby-on-rails rails three - Validate uniqueness of multiple columns

2 Answers

You can use validates to validate uniqueness on one column:

validates :user_id, uniqueness: {scope: :friend_id}

The syntax for the validation on multiple columns is similar, but you should provide an array of fields instead:

validates :attr, uniqueness: {scope: [:attr1, ... , :attrn]}

However, validation approaches shown above have a race condition and can’t ensure consistency. Consider the following example:

  1. database table records are supposed to be unique by n fields;

  2. multiple (two or more) concurrent requests, handled by separate processes each (application servers, background worker servers or whatever you are using), access database to insert the same record in table;

  3. each process in parallel validates if there is a record with the same n fields;

  4. validation for each request is passed successfully, and each process creates a record in the table with the same data.

To avoid this kind of behaviour, one should add a unique constraint to db table. You can set it with add_index helper for one (or multiple) field(s) by running the following migration:

class AddUniqueConstraints < ActiveRecord::Migration
  def change
   add_index :table_name, [:field1, ... , :fieldn], unique: true

Caveat : even after you've set a unique constraint, two or more concurrent requests will try to write the same data to db, but instead of creating duplicate records, this will raise an ActiveRecord::RecordNotUnique exception, which you should handle separately:

# writing to database
rescue ActiveRecord::RecordNotUnique => e
# handling the case when record already exists
fields validates_uniqueness_of scope

Is there a rails-way way to validate that an actual record is unique and not just a column? For example, a friendship model / table should not be able to have multiple identical records like:

user_id: 10 | friend_id: 20
user_id: 10 | friend_id: 20