rails validate uniqueness

To validate the uniqueness of a field in Ruby on Rails, you can use the validates method in the model file. First, specify the field you want to validate as unique using the uniqueness option. Then, you can add an error message if the validation fails using the message option. Here's an example:

class User < ApplicationRecord
  validates :username, uniqueness: { message: "This username is already taken" }
end

In this example, the User model validates the uniqueness of the username field and provides a custom error message if the validation fails.