Rubocop UniqueValidationWithoutIndex with scope

2.9k Views Asked by At

Rubocop has a uniqueness validator if the index is missing. So if validation for uniqueness is present, it searches for indexes in the schema. I don't get how does it work with scopes.

My model validations:

validates :name, presence: true, uniqueness: { case_sensitive: false, scope: :group_id }, length: { maximum: 20 }
validates :url, presence: true, uniqueness: { case_sensitive: false, scope: :group_id }

Added indexes on columns:

 add_index :rss_sources, [:url, :group_id], unique: true
 add_index :rss_sources, [:name, :group_id], unique: true

Rubocop is returning an error:

Rails/UniqueValidationWithoutIndex: Uniqueness validation should be with a unique index.

How to handle unique indexes when scope is present?

0

There are 0 best solutions below