Our app has a text field :schedule.
Originally, we stored data as a string.
The we changed the model to serialize :schedule, Hash
To fix legacy data, we simply want to replace the old (string) data with nil.
However, once we added serialization, attempting to write anything (a nil, or a replacement hash) to the field (if it has a string) throws the exception:
ActiveRecord::SerializationTypeMismatch: schedule was supposed to be a Hash, but was a String
Is there any way to override the serialization exception and force the field to a nil, or a replacement hash?
Perhaps there’s a way to bypass the serialization by doing an update to a field via SQL?
(I know we could use migrations to drop the column then re-add it, but am looking for a way to do this in a rake task.)
Have a look at
update_columnto quickly update all records in your database. This will send a simpleUPDATE table SET schedule = NULLto your database without respecting any validations or callbacks. Of course you’ll loose all your data in this column, but as far as I understand your description that’s not a problem, right?