Skip to content

Instantly share code, notes, and snippets.

@jmeirow
Created March 15, 2012 10:12
Show Gist options
  • Save jmeirow/2043465 to your computer and use it in GitHub Desktop.
Save jmeirow/2043465 to your computer and use it in GitHub Desktop.
Dump the data of ActiveRecord::Base based models to .yml files in the Rails db directory.
require 'active_record'
#
# When run from the root directory of a Rails project, this code will dump the data
# of all models inheriting from ActiveRecord. The data is dumped to .yml files in the
# db directory of the Rails app.
#
# This code is not very fast, as it starts up Rake for each model. It could use improvement.
#
# ------ Dependency on ar_fixtures -------
#
# This code assumes the Rails app has ar_fixtures ( https://github.com/topfunky/ar_fixtures )
# installed as a plugin.
#
# ar_fixtures is not actively maintained and is not Rails 3.x. However, making it Rails 3.x is quite painless.
# Simply replace 'RAILS_ROOT' with '::Rails.root.to_s' throughout all of the plugin files
# and it then works as expected.
#
#
# Usage: '$ ruby dump_models_to_files.rb'
#
#
Dir.foreach("./app/models/") do |f|
if f.end_with?(".rb")
full_name = "./app/models/#{f}"
require(full_name)
class_name = f.sub(/.rb/,"").camelcase
klass = Module.const_get(class_name)
if klass.superclass == ActiveRecord::Base
cmd = "rake db:data:dump MODEL=#{class_name}"
x = system(cmd)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment