您好我正在使用seed_dump gem从现有数据创建seeds.rb但是我坚持一件事我想为所有模型获取ID以及如何执行此操作例如当前如果我运行
rake db:seed:dump
我只是得到这样的代码
Product.create(title: "title", description: "text")
但我想要这个
Product.create(id: 1, title: "title", description: "text")
我该怎么做?
答案 0 :(得分:10)
rake db:seed:dump EXCLUDE=[]
这会覆盖[:id,:created_at,:updated_at]的默认排除,以便它包含id
答案 1 :(得分:1)
创建自己的导出。假设您的模型名称为Country
:
<强> LIB /任务/ export.rake 强>
namespace :export do
desc "Exports data for using in a seeds.rb."
task :seeds_format => :environment do
Country.order(:id).all.each do |country|
puts "Country.create(#{country.serializable_hash.
delete_if {|key, value| ['created_at','updated_at'].
include?(key)}.to_s.gsub(/[{}]/,'')})"
end
end
end
您可以使用此命令运行它:
rake export:seeds_format > db/seeds.rb