Hi,
As per the homework for Rails Active Record Basics it is required to have a
unique credit card number. The user can enter the number in three different
formats but the number gets stored in database only in one format. I have
written the following validation code:
# Validations on credit_card field
validates_uniqueness_of :credit_card,
:message=>"The credit card number is not unique"
#Validation on the credit_card field
validates_format_of :credit_card,
:with => /\A\d\d\d\d(-|\s)?\d\d\d\d(-|\s)?\d\d\d\d(-|\s)?\d\d\d\d\Z/,
:on => :create,
:message=>"Invalid credit card format"
I have also added following callbacks to save the number in one format in
the database:
# Strip everything but numbers
def before_save
self.credit_card = credit_card.gsub(/[^0-9]/, "") if
attribute_present?("credit_card")
end
# Strip everything but numbers
def before_create
self.credit_card = credit_card.gsub(/[^0-9]/, "") if
attribute_present?("credit_card")
end
Now the problem i'm facing is i'm able to save similar credit card numbers
which are entered in two different formats as '1234-2345-3456-4567' and
'1234234534564567' since the uniqueness validation is getting checked first
and then the callbacks are getting called.
Is there any solution for this problem?
Cheers,
Pallavi
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "ruby-on-rails-programming-with-passion" group.
To unsubscribe from this group, send email to
ruby-on-rails-programming-with-passion-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ruby-on-rails-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---