The rake task below searches for a file called test/ordered_fixtures.rb and loads that file. The file contains code that sets an environment variable specifying the load order for fixtures.
ENV["FIXTURE_ORDER"] = %w( entities interaction_sources interaction_devices payments interactions accounts employments enrollments payables receivables tenures wards ).join(' ')
The lib/tasks/fixtures.rake (rake load_fixtures_ordered) task first loads all fixtures specified via ordered_fixtures, and then loads all other fixtures not specified as being ordered so it should work for you even if you do not create an ordered_fixtures file.
require File.expand_path(File.dirname(__FILE__) + "/../../config/environment") require File.expand_path(File.dirname(__FILE__) + "/../../test/ordered_fixtures") ENV["FIXTURE_ORDER"] ||= "" desc "Load fixtures into #{ENV['RAILS_ENV']} database" task :load_fixtures_ordered => :environment do require 'active_record/fixtures' ordered_fixtures = Hash.new ENV["FIXTURE_ORDER"].split.each { |fx| ordered_fixtures[fx] = nil } other_fixtures = Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.{yml,csv}')).collect { |file| File.basename(file, '.*') }.reject {|fx| ordered_fixtures.key? fx } ActiveRecord::Base.establish_connection(ENV['RAILS_ENV']) (ordered_fixtures.keys + other_fixtures).each do |fixture| Fixtures.create_fixtures('test/fixtures', fixture) end unless :environment == 'production' # You really don't want to load your *fixtures* # into your production database, do you? end
The code may not be the best but it Works For Me.
Nice one, thanks.
I have an alternative solution here http://techpolesen.blogspot.com/2007/04/rails-fixture-tips.html, which takes some other stuff into account:
* that individual records in a yaml can have interdependencies
* loading of binary files
* can delete fixtures in correct order too
Kind regards, Per