Well, I see a couple things with this code that I don't believe have anything to do with the problem, but should be addressed. First, you only need to use one of the callbacks. The before_save gets called on create (a new record) or update, so using the before_create is redundant (see http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html). The second issue I see is that gsub has an error in it is should be credit_card.gsub(/[-]/, " ") so that "1111-2222-3333-4444" wil return "1111 2222 3333 4444" instead of "1111222233334444". The last issue is that the validation happens BEFORE the before_save callbacks see the order on the page referenced above. That means that if "1111 2222 3333 4444" is already in the database that a credit card trying to be saved as "1111-2222-3333-4444" will not raise the uniqueness error because the "-"s are not stripped out BEFORE the validation check
As for why it is saving the record to the database, I do not know. This code (even with the issues) should not save the secord record. You are sure that you have confirmed it is IN the database and that you are not looking at your unsaved user object? There are some instances where validates_uniqueness_of does not actually happen but I doubt that would happen in your little development environment (see http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M002110 ). On Thu, Apr 23, 2009 at 1:29 PM, Bernard Coll <[email protected]> wrote: > 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 -~----------~----~----~----~------~----~------~--~---
