Jim Lindley Notes

Foxy Fixtures

October 27th, 2007

Just added to edge Rails is an update that makes fixtures worth using again. Instead of manually specifying the keys of associated fixtures or created/modified times, like this:

# in users.yaml

  jdoe:
    id: 1
    name: John Doe
    created_at: <%= Time.now %>
    modified_at: <%= Time.now %>

# in photos.yaml

  sunset_photo:
    id: 42
    title: Photo of a Sunset
    user_id: 1
    created_at: <%= Time.now %>
    modified_at: <%= Time.now %>

You can just give the name of the associated fixture and Rails will figure out keys for you:

# in users.yaml

  jdoe:
    name: John Doe

# in photos.yaml

  sunset_photo:
    title: Photo of a Sunset
    user: jdoe # user not user_id

For has_and_belongs_to_many assocations, you can specify the relations with a comma separted list of references to the associated fixtures:

# in tags.yaml

  red:
    name: Red
    items: fire_hydrant, rover

Changeset #8036, RDoc

Sorry, comments are closed for this article.