I have some code that simply accesses a datetime field so activerecord converts it to a Time object automatically when I read it:
@some_appointment.some_time
The problem is that sometimes the "some_time" datetime column has bad data. "0209-12-20" instead of "2009-12-20". This causes to Ruby to throw a "year too big to marshal" error just from accessing, and I can't seem to catch it as the error is not an exception. Given that there is some invalid data in my database, I want to try and make the time object myself by reading the datetime string and using Time.parse so I can catch an exception if the time cannot be parsed. How can I accomplish this? Is there a better way?
You might try attribute_for_inspect, as in:
SomeModel.find(:first).attribute_for_inspect 'some_time'
#=> ""2009-02-23 23:15:24""
You might also consider just writing a stand-alone script that doesn't use activerecord to go over your data in the database to normalize it or something. That would probably be faster than coding the special-case of bad data into your app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With