zerosum dirt(nap)

evolution through a series of accidents

zerosum dirt(nap)

Storing YAML in YAML Fixtures

January 10, 2008 by nap · Comments

If you ever find yourself in a situation where you have an attribute value that needs to be serialized (stored as YAML) and used within a (YAML) fixture, you can do something like:

FancyWidget:
  id: 1
  name: FancyWidget
  serialized_hash: "<%= {:abc => {:xyz => 'foo'}}.to_yaml %>"

Or, if you want to embed YAML directly you can do:

FancyWidget:
  id: 1
  name: FancyWidget
  serialized_hash: |
    ---
    :abc: 
      :xyz: foo

This is probably old hat to a lot of you, but I figured I’d doc it here since the solution felt somewhat non-obvious at first. Rails stores serialized attributes in your database this way, so you might run into this if you’re using ActiveRecord’s serialize method.

blog comments powered by Disqus