Reputation: 10673
In my Rails 4 app I have an email field. My model ensures that no duplicate values are stored using the following:
validates :email, presence: true, uniqueness: { case_sensitive: false }
This has served me well while I wasn't bothered about confirming email addresses but now I want to confirm them before they are able to login. This means that I only want to confirm uniqueness if another field confirmed
is true.
Is there an in-built way to tackle this at all or will it be my own validation rule that's required?
Upvotes: 1
Views: 103
Reputation: 14038
You can pass options such as a record set to a uniqueness validator like so:
validates_uniqueness_of :email, conditions: -> { where(confirmed: true) }
Then it will only enforce uniqueness against confirmed records.
Upvotes: 1
Reputation: 451
I believe this is what you want:
http://edgeguides.rubyonrails.org/active_record_validations.html#using-a-symbol-with-if-and-unless
Upvotes: 0