# Help in validates\_uniqueness\_of helper

**URL:** <https://www.ruby-forum.com/t/help-in-validates-uniqueness-of-helper/158183>\
**Category:** Rails\
**Created:** [January 20, 2009, 8:44am UTC](https://www.ruby-forum.com/t/help-in-validates-uniqueness-of-helper/158183 "2009-01-20T08:44:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocknrollcar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rocknrollcar](https://www.ruby-forum.com/u/rocknrollcar)\
**Post date:** [January 20, 2009, 8:44am UTC](https://www.ruby-forum.com/t/help-in-validates-uniqueness-of-helper/158183/1 "2009-01-20T08:44:50Z")

</div>

Hello. In my rails app, the user has to create an account. There are two  
fields email and alt\_email. The both should be unique. I used  
validates\_uniqueness\_of helper to validate the fields. Now my problem is  
the same user can create a fake account by interchanging the values of  
email and alt\_email fields. Please help in solving this issue. Thanks in  
advance

---

<div class="post-metadata">

**Author:** ![rocknrollcar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rocknrollcar](https://www.ruby-forum.com/u/rocknrollcar)\
**Post date:** [January 20, 2009, 8:00pm UTC](https://www.ruby-forum.com/t/help-in-validates-uniqueness-of-helper/158183/2 "2009-01-20T20:00:19Z")

</div>

Not sure in what sense interchanging the values of  
email and alt\_email fields would create a fake account.

If you mean with the same two emails you can have two valid accounts  
yes that is right. You have to decide if that is a problem.

If it is i guess it is also a problem to associate the same email to  
two accounts, whatever the other address is

An obvious solution may be (with any number of alternative emails)  
to have a separate Email table

class User

# (with any number of alternative emails)

has\_many :emails

# with two emails

has\_one :email  
has\_one :alt\_email, :class =\> ‘Email’

class Email  
belongs\_to :user  
acts\_as\_list :scope =\> user\_id  
validates\_uniqueness\_of :address

Alternatively, if email and alt\_email are just text columns in your  
user table, just write a validation function  
that checks uniqueness on the two columns (both compulsary).

validates\_each :email, :alt\_email do |record,attr,value|  
if value.blank?  
record.errors.add attr, “cannot be blank”  
else  
email\_taken = find(:conditions =\> [‘(users.email = ? OR  
users.alt\_email = ?) AND users.id != ?’, value, value, record.id])  
record.errors.add attr, “(#{value}) is already taken” if  
email\_taken  
end  
end

Hope this helps  
Vik

On Jan 20, 7:44 am, Rock R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![rocknrollcar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@rocknrollcar](https://www.ruby-forum.com/u/rocknrollcar)\
**Post date:** [January 21, 2009, 12:35am UTC](https://www.ruby-forum.com/t/help-in-validates-uniqueness-of-helper/158183/3 "2009-01-21T00:35:03Z")

</div>

Thank you. But i have finished all the works. SO i can’t do your first  
method. But i did a custom validation fn and it works. Thanks anyway

tron wrote:  
other address is

> belongs\_to :user  
> acts\_as\_list :scope =\> user\_id  
> validates\_uniqueness\_of :address
> 
> Alternatively, if email and alt\_email are just text columns in your  
> user table, just write a validation function  
> that checks uniqueness on the two columns (both compulsary).
> 
> validates\_each :email, :alt\_email do |record,attr,value|  
> if value.blank?  
> record.errors.add attr, “cannot be blank”  
> else  
> email\_taken = find(:conditions =\> [‘(users.email = ? OR  
> users.alt\_email = ?) AND users.id != ?’, value, value, record.id])  
> record.errors.add attr, “(#{value}) is already taken” if  
> email\_taken  
> end  
> end
> 
> Hope this helps  
> Vik
> 
> On Jan 20, 7:44ï¿½am, Rock R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
