I'm using Ruby on Rails 5. I have the following code in my model ...
class User < ActiveRecord::Base
...
attr_accessor :dob_string
def dob_string
@dob_string || (self.dob ? self.dob.strftime('%m/%d/%Y') : "")
end
def dob_string=(dob_s)
date = dob_s && !dob_s.empty? ? Date.strptime(dob_s, '%m/%d/%Y') : nil
self.dob = date
rescue ArgumentError
errors.add(:dob, 'The birth date is not in the correct format (MM/DD/YYYY)')
@dob_string = dob_s
end
Despite my instructions, sometimes users enter the date in the form MM-DD-YYYY. So what I want to do is allow my model to accept both formats, MM/DD/YYYY or MM-DD-YYYY, but not mixed, that is MM/DD-YYYY shoud still be illegal. How do I modify my model so that it can accept either format?