I want to interrupt the default registration action in my rails application, changing email validation. All I need is to have email field unique ONLY if that user is already registered AND has role 'X'.
I've tried the following but my application returns "Email has already been taken" message:
validation:
validates_uniqueness_of :email, {allow_blank: true, if: :already_exists_as_x?}
def already_exists_as_x?
User.where(email: self.email, role: :X).exists?
end
(I will be glad for any help)
Assuming your model includes :validatable module of Devise, you can remove this module from your model and write your own validations as you desire.
In your model (say user.rb) your devise call contains :validatable like:
Remove or comment :validatable from this call, then your own validations should work as you expected.