Hi all,

im doing the lab on active record. im testing records with same credit card
#. im checking for uniqueness on that field.

In rail console i add the following records:

user = User.create(:name => "two", :hobby => "swimming", :age => 18, :email
=> "[email protected]", :credit_card => "2222 2222 2222 2222")
user = User.create(:name => "three", :hobby => "swimming", :age => 18,
:email => "[email protected]", :credit_card => "2222 2222 2222 2222")

i then type user.save! to see the message for the last record as it has the
same credit card # as the previous one and indeed it returns false and the
message tells it is not unique. However, the problem i have is that the
record STILL GET ADDED TO THE TABLE!

What's wrong?

Below is the full validation on that field (credit_card. )
===========================================================================================

  # Validations on credit_card field
  validates_uniqueness_of :credit_card,:message=>"The credit card is not
unique"

  # Remove "-" if found in the credit_card field
  def before_save
    self.credit_card = credit_card.gsub(/[-]/, "") if
attribute_present?("credit_card")
  end

   # Remove space if found in the credit_card field
  def before_create
     self.credit_card = credit_card.gsub(/[ ]/, "") if
attribute_present?("credit_card")
  end

  # Validation on the credit_card field
   validates_format_of :credit_card,
     :with => /\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?/,
     :on => :create,
     :message => "Invalid credit card format"

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to