FixtureSeed is a Rails gem that automatically loads YAML fixture files from the db/fixtures/ directory before running rails db:seed. It loads the fixtures in alphabetical order and handles foreign key constraint errors by retrying failed inserts after all other fixtures are loaded.
Add this line to your application's Gemfile:
gem 'fixture_seed'And then execute:
$ bundle installOnce the gem is installed, it will automatically hook into Rails' db:seed task. You don't need to modify your seeds.rb file.
Place your YAML fixture files in the db/fixtures/ directory:
db/
fixtures/
posts.yml
users.yml
The fixture files should be named after the table they correspond to (e.g., users.yml for the users table).
The content of the fixture files should follow this format:
# users.yml
user1:
id: 1
name: "John Doe"
email: "john@example.com"
user2:
id: 2
name: "Jane Smith"
email: "jane@example.com"The labels (e.g., user1, user2) should follow the pattern of the table name in singular form followed by a number.
For dynamic fixture generation, you can use ERB templates in your YAML files:
# users.yml
<% 10.times do |i| %>
user<%= i + 1 %>:
id: <%= i + 1 %>
name: "User <%= i + 1 %>"
email: "user<%= i + 1 %>@example.com"
created_at: <%= Time.current.to_s(:db) %>
<% end %>ERB templates have access to the full Rails environment, allowing you to use helpers, constants, and other Ruby code to generate dynamic fixture data.
FixtureSeed supports order-independent loading, which means you don't need to worry about the loading order of your fixture files based on database relationships.
- Fixtures are initially loaded in alphabetical order by filename
- When a fixture fails to load due to foreign key constraints, it's automatically retried later
- The gem continues processing other fixtures and comes back to failed ones
- This process repeats until all fixtures are loaded or no progress can be made
- No manual dependency management: You don't need to rename files or organize them based on foreign key relationships
- Simplified fixture organization: Focus on logical grouping rather than loading order
- Robust loading: Handles complex relationships automatically
- Error resilience: Temporary constraint violations don't stop the entire process
Even if your fixtures have dependencies like this:
db/fixtures/
comments.yml # depends on posts and users
posts.yml # depends on users
users.yml # no dependencies
FixtureSeed will automatically handle the loading order, ensuring users.yml loads first, then posts.yml, and finally comments.yml, regardless of alphabetical order.
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
Bug reports and pull requests are welcome on GitHub at https://github.com/komagata/fixture_seed.
The gem is available as open source under the terms of the MIT License.