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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-11 16:05 UTC
Re: Compute date during before_validation_on_create
On Dec 11, 9:06 am, nuno <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 ?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> > Thanks for your ideas > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Apparently Analagous Threads
- calculating the difference between days?
- ez_where query question
- before_create is after_validatation :on => :create , when it should be before_validation :on => :create
- replace_html not working
- Observer behavior differences between DEV and TEST environments ?