tommyd456
tommyd456

Reputation: 10673

Rails Model - Confirm Email Address

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

Answers (2)

Matt
Matt

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

Related Questions