I have this line to check the uniqueness before saving and it works fine for me. validates_uniqueness_of :credit_card, :if => :before_save, :message => "The credit card number already exists"
Cheers, Sun --- On Mon, 22/9/08, Juninho <[EMAIL PROTECTED]> wrote: From: Juninho <[EMAIL PROTECTED]> Subject: [rails-development] Re: Rails Active Record Basics Homework doubt. To: "ruby-on-rails-programming-with-passion" <[email protected]> Date: Monday, 22 September, 2008, 4:14 PM I'm also having the same problem. When I try to add the following: User.create(:name => "Guilherme", :hobby => "tennis", :age => 24, :email => "[EMAIL PROTECTED]", :credit_card => "1234 1234 1234 1234") the uniqueness does not work if I try to add a new record and just change, for example, the name. It will only work if type a credit card using format #3. So it's not checking uniqueness for format #1 and format #2. Here's what I'm using to do those checks: class User < ActiveRecord::Base # Strip everything but numbers def before_create self.credit_card = credit_card.gsub(/[^0-9]/, "") if attribute_present?("credit_card") end # Strip everything but numbers def before_save self.credit_card = credit_card.gsub(/[^0-9]/, "") if attribute_present?("credit_card") end # Validation on the email field validates_format_of :email, :with => /\A([EMAIL PROTECTED])@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create, :message=>"Invalid email format" # Validations on credit_card field validates_uniqueness_of :credit_card, :message=>"is not unique" # Validation on the credit_card field validates_format_of :credit_card, :with => /(([0-9]{4}[-\s]{0,3}){3}(([0-9]{4}){1}))/i, :on => :create, :message=>"Invalid Credit Card format" end It has been some time that I'm working on it, but this is not working for me, so if anyone has any idea on what to do, it will be great. Thanks & Regards, Guilherme O. Franchi Junior 2008/9/22 Daniel Freire <[EMAIL PROTECTED]> That made no difference...Any more ideas? On Thu, Sep 11, 2008 at 11:13 PM, Presario 6331RSH HAM <[EMAIL PROTECTED]> wrote: > Observe that when I create a User with the credit_card = "1234 1234 1234 > 1234" (format #2), the validates_uniqueness_of doesn't work. The samething > happens when I use format #1, but with format #3 it works! > I think that before_save and before_create callbacks are running after the > validates_uniqueness_of :credit_card. I think you mean that the callbacks are running before the validation. I am having the same problem. I enter user = User.new(:name => "unique", :credit_card => "usedbefore number") and it gets saved because the callbacks have converted it to format#3 so therefore it is unique now. Will changing the order of validates_uniqueness_of :credit_card, :message => "The credit card is not unique." and ...callbacks make any difference? -- --------------------------------------------------------------------------- Daniel Almeida Freire --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/ruby-on-rails-programming-with-passion?hl=en?hl=en -~----------~----~----~----~------~----~------~--~---
