7

In Rails 3.2, is it possible to have multiple fixture files for a given ActiveRecord object?

The client is requiring the test data be written in fixtures but also wants them to be manageable. I'd like to split up the fixtures a bit by introducing a 2nd set that the originals would include/require/render whatever.

I haven't been able to find anything via Google on how to do it and fixtures are not my cup of tea. Thanks in advance.

Chance
  • 11,043
  • 8
  • 61
  • 84

1 Answers1

6

Seems to me like what you want to do is include a YAML file from another YAML file. Here's a question which covers how to do that: How to include a YAML file inside a YAML file?

Since fixtures already have ERB, it should be as simple as:

<%= IO.read(Rails.root.join "test/other_fixtures/fixture_to_load.yml") %>

Just make sure that the fixtures are outside of the primary fixture directory or the mechanism for loading fixtures will also attempt to map them to a model.

If you need ERB inside of the fixture, wrap it in ERB.new, such as:

<%= ERB.new(IO.read(Rails.root.join "test/other_fixtures/fixture_to_load.yml")).result %>
Community
  • 1
  • 1
alexpls
  • 1,914
  • 20
  • 29
  • Ah. Thanks. This actually led me to a really simple solution. Since fixtures already have ERB, it as just this: <%= IO.read(Rails.root.join "test/other_fixtures/fixture_to_load.yml") %> – Chance Oct 24 '12 at 02:53
  • You're absolutely right - I didn't realize it would be this straight forward! – alexpls Oct 24 '12 at 08:11