YAML ActiveRecord Dump


/ Published in: Ruby
Save to your folder(s)

I didn't create this but I use it all the time. Just drop it in lib/tasks/fixtures.rake


Copy this code and paste it in your HTML
  1. namespace :db do
  2. namespace :fixtures do
  3.  
  4. desc 'Create YAML test fixtures from data in an existing database.
  5. Defaults to development database. Set RAILS_ENV to override.'
  6. task :dump => :environment do
  7. sql = "SELECT * FROM %s"
  8. skip_tables = ["schema_info"]
  9. ActiveRecord::Base.establish_connection(RAILS_ENV)
  10. (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
  11. i = "000"
  12. File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|
  13. data = ActiveRecord::Base.connection.select_all(sql % table_name)
  14. file.write data.inject({}) { |hash, record|
  15. hash["#{table_name}_#{i.succ!}"] = record
  16. hash
  17. }.to_yaml
  18. end
  19. end
  20. end
  21. end
  22. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.