We were faced with the interesting problem of having a database column that stored YAML representations of data. To be more precise, that wasn’t the problem, it was how to load YAML data in using Rails fixtures, which themselves are in YAML (or CSV, but we prefer YAML).
After the usual Googling, I eventually found a few sites that gave me some hints. Apologies for not referencing those sites but now can’t find them again (and it took me a couple of hours the first time). So, in the hope that others who follow behind me have another source of assistance I thought it would be useful to blog it here.
If you have a model with an id and a column full of yaml, you would construct the fixture this way:
[code lang="ruby"]SOME_MEANINGFUL_NAME_FOR_MY_DATA_HERE: id: 1 yaml_column_name_here: | foo: "man choo" bar: "of soap"[/code]
In this case we have a fixture with a value for yaml_column_name_here with a YAML object with string values for foo and bar as a hash. Note that the pipe character starts the YAML block, then the subsequent rows are at one extra level of indentation that the fixture data itself.
Alternatively you could also do:
[code lang="ruby"]ANOTHER_MEANINGFUL_NAME_FOR_MY_DATA_HERE: id: 2 yaml_column_name_here: <%= Time.now.to_date_to_yaml %>[/code]
In this instance we are saving a YAML value of the current date.