In my migration file I have a variable whose value should be unique or nil. How can I achieve such? My current setup generates all sorts of validation errors, I think because nil values are not unique and in the current set up it wants to see a unique value.
I currently have:
Migration file:
class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string   :my_var
      ...
    end
  end
end
class AddIndex < ActiveRecord::Migration
  def change
    add_index :users,  :my_var,    unique: true
  end
end
Model file:
validates :my_var, uniqueness: true
Is there a way to allow it to be nil, to require a unique value if it has a value, and to make it an index?
 
    