DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Loading Data From Fixtures In Development Mode

06.05.2005
| 18887 views |
  • submit to reddit
        By <a href="http://one.textdrive.com/pipermail/rails/2005-March/003844.html">Tim Bates.</a>

desc "Load fixtures data into the development database"
task :load_fixtures_data_to_development do
   require 'active_record/fixtures'
   ActiveRecord::Base.establish_connection(
       ActiveRecord::Base.configurations["development"])
   Fixtures.create_fixtures("test/fixtures",
       ActiveRecord::Base.configurations[:fixtures_load_order])
end
    

Comments

Snippets Manager replied on Sat, 2006/04/15 - 8:53am

This is obsolete with Rails 1.1. It can be done now by rake db:fixtures:load

Snippets Manager replied on Mon, 2012/05/07 - 1:12pm

For some reason I got an error using the :fixtures_load_order parameter. It may be related to me using Sql Server as the database. Here is the task definition I used to get it to work on my system: desc "Load fixtures data into the test database" task :load_test_database => :environment do require 'active_record/fixtures' ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["test"]) fixture_dir = "test/fixtures" tables = Dir["#{fixture_dir}/*.yml"] tables.collect! {|t| File.basename(t, '.yml')} Fixtures.create_fixtures(fixture_dir, tables) end

Snippets Manager replied on Mon, 2012/05/07 - 1:12pm

I had to change your code to make it work for me, as it wouldn't properly read my environment at first: desc "Load fixtures data into the development database" task :load_fixtures_data_to_development do require 'active_record/fixtures' RAILS_ENV = 'development' require File.dirname(__FILE__) + '/config/environment' ActiveRecord::Base.establish_connection() Fixtures.create_fixtures("test/fixtures", %w(pages notes users)) end

Snippets Manager replied on Mon, 2012/05/07 - 1:12pm

The reverse would be cool too. Create fixtures automatically based on what you're development database currently looks like.