Never been to DZone Snippets before?

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

Loading data from fixtures in development mode (See related posts)

By Tim Bates.

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 on this post

d723 posts on Jun 18, 2005 at 00:16
The reverse would be cool too. Create fixtures automatically based on what you're development database currently looks like.
lorenzk posts on Jul 02, 2005 at 14:34
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
btakita posts on Nov 28, 2005 at 19:34
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
Sash posts on Apr 15, 2006 at 09:55
This is obsolete with Rails 1.1. It can be done now by
rake db:fixtures:load

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts