I have cracked it!

Instead of using validates_format_of, I use validates_each (
http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M001045),
which requires a block.
In the block, I validate the value (of the credit card) against the regular
expression. Then I also convert the value to format 3 inside the block.

I also define validates_uniqueness_of, which will then validate against the
converted value.

So eventually I don't even need to call before_save or before_create.

I have run against Daniel's samples and it can detect duplicates entered
with different formats.

I hope this is a valid solution to this homework.

Cheers,
Alex Wei

On Mon, Sep 22, 2008 at 11:29 PM, Daniel Freire <[EMAIL PROTECTED]>wrote:

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

Reply via email to