Kurt Mueller
Kurt Mueller

Reputation: 3224

Rails: Uniqueness validator not respecting scope

I ran into this issue and wanted to post this so that way others don't spend so much time banging their head against a wall like I did.


I have a User model. On the User model, I have uniqueness validate on the email attributed scoped to its type attribute. See below:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  validates :email,
    uniqueness: {
      scope: :identifier
    }
end

However, the UniquenessValidator doesn't respect the fact that email uniqueness validator is scoped to its identifier attribute.

What's going on here?

Upvotes: 0

Views: 341

Answers (1)

Kurt Mueller
Kurt Mueller

Reputation: 3224

What was happening:

Devise's validatable module overrode my uniqueness validator for email. Simply removing this module allowed me to validate my email's uniqueness scoped to its identifier attribute.

class User < ActiveRecord::Base

  # removed :validatable

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable


  validates :email,
    uniqueness: {
      scope: :identifier
    }
end

Upvotes: 1

Related Questions