Skip to content

Files

Latest commit

 

History

History
26 lines (17 loc) · 966 Bytes

Rails-UniqueValidationWithoutIndex.md

File metadata and controls

26 lines (17 loc) · 966 Bytes

Pattern: Unique validation without index

Issue: -

Description

When you define a uniqueness validation in Active Record model, you also should add a unique index for the column. There are two reasons:

  • duplicated records may occur even if Active Record's validation is defined;
  • it will cause slow queries. The validation executes a SELECT statement with the target column when inserting/updating a record. If the column does not have an index and the table is large, the query will be heavy.

Examples

# bad - if the schema does not have a unique index
validates :account, uniqueness: true

# good - if the schema has a unique index
validates :account, uniqueness: true

# good - even if the schema does not have a unique index
validates :account, length: { minimum: MIN_LENGTH }

Further Reading