namespace :db do
namespace :fixtures do
desc 'Create YAML test fixtures from data in an existing database.
Defaults to development database. Set RAILS_ENV to override.'
task :dump => :environment do
sql = "SELECT * FROM %s"
skip_tables = ["schema_info"]
ActiveRecord::Base.establish_connection(RAILS_ENV)
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
i = "000"
File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|
data = ActiveRecord::Base.connection.select_all(sql % table_name)
file.write data.inject({}) { |hash, record|
hash["#{table_name}_#{i.succ!}"] = record
hash
}.to_yaml
end
end
end
end
end
code-snipplet|| put table content into yaml
最新推荐文章于 2025-08-14 09:06:11 发布
本文介绍了一种使用 Ruby on Rails 从现有数据库中创建 YAML 格式的测试固定装置的方法。默认针对开发数据库,可通过设置 RAILS_ENV 来指定其他环境。该过程涉及遍历数据库表并导出其内容为 YAML 文件。
9465

被折叠的 条评论
为什么被折叠?



