adding additional validation to email uniqueness #242

dhruvg opened this Issue Mar 17, 2011 · 1 comment


None yet
2 participants

dhruvg commented Mar 17, 2011

I am maintaining another model for each user called secondary_email where users can basically tag on multiple email addresses to their account. I want to modify my User model to also check that a new user's email is not an active secondary email... Here is the code I have so far:

acts_as_authentic do |c|
:minimum => 6, :maximum => 40)
c.merge_validates_uniqueness_of_email_field_options(:if =>

returns true if the given email is active, false otherwise

SecondaryEmail.where("email = :email and pending = 0",
:email => email).length > 0

It doesn't seem to be passing my test case:

it "should reject if email is an active secondary email" do
@user = User.unsafe_create(@attr)
secondary_email = Factory(:secondary_email, :user_id => @user[:id])
User.unsafe_create(@attr.merge(:email =>
secondary_email[:email])).should_not be_valid

The Factory which creates the SecondaryEmail appropriately sets pending to 0 which is checked in the active? function:

Secondary Email

Factory.define :secondary_email do |se|
se.sequence(:email) { |n| "voltaire_se#{n}"}
se.pending "0"
se.user_id "1"
se.crypt "random"
se.num_verifications "5"

So, I am pretty sure my test case is correct. I think the issue likes in the acts_as_authentic block -- is the code above the right way to go about it? What is the correct approach?


Your :if condition will evaluate to :true or :false, neither of which are methods on the model. This may be of some help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment