Created
March 15, 2012 10:12
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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