Compute date during before_validation_on_create

Hello,

  • the form posted by the user have a date field
  • this date is begin_date, I need to compute end_date
  • so, added callback before_validation_on_create
  • in this method I do :
    end_date = begin_date >> 12
  • unfortunately, beeing in before_validation the actual self.begin_date
    have not been type casted - It’s always a string
  • unfortunately again, I can’t put this code in before_create because
    the validation will not pass because (before_create is called after
    validation checks)

I could remove the validation against end_date but I don’t like this…
What will happen if the computed end_date goes wrong ?

Thanks for your ideas

On Dec 11, 9:06 am, nuno [email protected] wrote:

the validation will not pass because (before_create is called after
validation checks)

I could remove the validation against end_date but I don’t like this…
What will happen if the computed end_date goes wrong ?

I would suggest removing the end_date validation and adding
comprehensive tests for that method. You validate the user input
(begin_date) and if you have confidence in your end date creation
method (because you have tested it well), then it will be no more
subject to failure than any other method in your application.

Your end_date_creation method should make it impossible for any valid
begin_date to produce an invalid end_date.

OR, if you insist on validating the end_date, there doesn’t seem to be
any reason you can’t calculate the correct date from the pre-processing
string type begin_date and calculate from there.

-r