1

I'm trying to populate a development database with fixtures for testing purposes.

I am populating a model's data using multiple fixture files in different directories. However, it seems like my fixture loading is not additive. That is, my table gets re-initialized from scratch every time.

This is my code loop:

Dir[Rails.root.join('db', 'seeds', 'dev', '**/*.yml')].each do |file|
  puts "Loading #{file}"
  ActiveRecord::FixtureSet.create_fixtures(File.dirname(file), File.basename(file, '.yml'))
end

For example, my model Article has many fixture files that will write to it. i.e. news.yml, tech.yml, recipies.yml... etc.

Every one of those fixture files I have:

_fixture:
  model_class: Article

...rest of fixtures...

In the beginning of the file.

When I run my task, only the last Article fixture data gets retained.

How can I utilize all of the Article fixtures to initialize the model? (all of news.yml, tech.yml, recipies.yml)

mhz
  • 1,019
  • 2
  • 8
  • 30

1 Answers1

0

You could try sourcing the fixtures directly from the official fixtures file:

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

In this case, a file like articles.yml might just become a shell file with IO.read statements to decompose into multiple files.

Source

mahemoff
  • 44,526
  • 36
  • 160
  • 222