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
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. [email protected]
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 :addressAlternatively, 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
endHope this helps
VikOn Jan 20, 7:44�am, Rock R. [email protected]