What's the cleanest way to convert data (field values) when storing/loading into/from a database.
I usually convert the values the user has entered in the model class:
def date_str
format_date(self.date)
end
def date_str=(str)
self.date = DateParser.parse(str)
end
(Note: The internal date parser is not sufficient, it doesn't cover many of our locale date notation styles, anyway).
This works already pretty fine, but I doesn't let me check for validity the usual RoR way because we don't store the string the user has entered. I usually let the parser function return nil if anything was wrong with the date, but this isnt very user-friendly – I cant really tell if the user hasn't entered anything or just a invalid value.
I'm looking for some way to integrate the conversion better into the A::R world where I can produce some useful error message if the conversion fails. Best would be if I could use the same facilities as the A::R::Adapter uses internally.
Has anybody some good ideas? What are other approaches?