Forum: Ruby on Rails Help in validates_uniqueness_of helper

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rock R. (Guest)
on 2009-01-20 09:44
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
tron (Guest)
on 2009-01-20 21:00
(Received via mailing list)
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>
Rock R. (Guest)
on 2009-01-21 01:35
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>
This topic is locked and can not be replied to.