I have code that just accesses the datetime field, so activerecord will automatically convert it to a Time object 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 leads to the fact that Ruby throws the error "the year is too big for the marshal" only from access, and I can not understand that the error is no exception. Given that there is some invalid data in my database, I want to try to make the time object itself by reading the datetime string and using Time.parse so that I can catch an exception if the time cannot be parsed. How can i do this? Is there a better way?
You can try attribute_for_inspectas in:
attribute_for_inspect
SomeModel.find(:first).attribute_for_inspect 'some_time' #=> ""2009-02-23 23:15:24""
, script, activerecord , - . , , .
, , , attributes_before_type_cast? , Rails , , Rails , .
attributes_before_type_cast
:
@some_appointment.attributes_before_type_cast['some_time']
You could override the default accessors like this in your model:
def some_time read_attribute(:some_time) rescue # Try to parse this guy self.some_time_before_type_cast end
Source: https://habr.com/ru/post/1727306/More articles:Get out of control - wpfСоображения для очень больших таблиц SQL? - mysqlIs there a way to handle Silverlight deep binding without the "#" displayed in the url? - silverlightAn object oriented way of writing HTML as a string? - htmlОсновной вопрос группировки массива PHP MySQL - arraysHow to change a value in a JavaScript array with multiple values - javascriptLinux threading performance is very fast under GDB, but very slow, otherwise multithreadingHow to use Python tkSimpleDialog.askstring - pythonJava Media Framework: извлечение аудиоинформации из mp3 файла - javaOutOfMemoryException when I hit 1 GB - .netAll Articles