> validates_uniqueness_of :credit_card, :if => :before_save ..
I have seen this expression in other posts and I could not figure out what
the logic is, because:
1) the *if => proc/method* is intended to run the validate only if the
method returns true. So the logic is to validate something only if some
other condition (probably on another attribute or model) is true.
2) I am instead intrigued by '*:if => before_save*'. The method *before*_save
is usually defined to perform some action just before saving the record. I
can't figure out what is the logic of calling it within a validation (sure,
you can make it check something and return true... but what is the overall
design? the *before_save* is not intended to do validations, but to perform
actions! here the programmer uses it to run a validation from within another
validation... I give up).
> Did I get the point of the exercise or is there more I need to get?
No (to the 1st question), and Yes (to the 2nd). Let's analyze the problem
systematically:
First, the requirements are:
a) to accept 3 different formats of credit cards
b) to store a specific format in the database
c) to check that credit cars are unique.
Now, what you called the *point* of the exercise is this paradox:
1) it would seem that *before_save* could convert the format to the one
chosen for the database
2) but.. we also need to check the format, and the uniqueness
3) however *validate_uniqueness* runs *before* the* before_save* callback...
4) but then, how can *validate_uniqueness *check the uniqueness of something
that is still expressed in different formats?
It reminds at first view an Escher's construction... but the problem is now
defined,
Raul
On Fri, Oct 31, 2008 at 5:38 PM, anne001 <[EMAIL PROTECTED]> wrote:
>
> Sorry, I am a bit behind.
>
> OK, so the problem is
> Because before_save changed the data stored in the data base, the
> record xxx-xxx... looks different from xxxxxx in the database, to the
> validation.
>
> validates_uniqueness_of :credit_card, :if => :before_save, :message =>
> "The credit card number already exists"
> :if - Specifies a method, proc or string to call to determine if the
> validation should occur (e.g. :if => :allow_validation, or :if =>
> Proc.new { |user| user.signup_step > 2 }). The method, proc or string
> should return or evaluate to a true or false value.
>
> http://ar.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html
>
> So with this :if condition, before_card is called before the
> validation, returns "true", and then the validation is called.
>
> I found this an interesting test example
> http://dev.rubyonrails.org/ticket/7377
>
> their conclusion is to use a call back before validation instead of
> before_save and before_create.
>
> It was interesting that with the :if => before_save condition present,
> but no "before_save" method defined,
> the before_validation never runs.
>
> Did I get the point of the exercise or is there more I need to get?
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---