从数据库中已有的数据创建种子文件

时间:2022-06-24 16:46:12

I'm using Rails 3.0.3 and have data for my "categories" table already in the database, but want to create a seed file from it. Is there any rake task that will generate the seeds.rb format for me from this table?

我使用的是Rails 3.0.3,数据库中已经有我的“categories”表的数据,但我想从中创建一个种子文件。有任何耙子任务可以产生种子吗?从这个表格里给我rb格式?

4 个解决方案

#1


58  

There is a gem called seed_dump, which will do exactly what you want:

有一种叫做seed_dump的宝石,它可以做你想做的事情:

#2


27  

Not sure about any existing rake tasks, but you can try running something like this in the rails console & paste the results into your seeds.rb file

不确定现有的rake任务,但您可以尝试在rails控制台中运行类似的操作,并将结果粘贴到您的种子中。rb文件

(warning: dirty & untested)

(警告:脏和未测试)

c = Category.all

c.each do |cat|
  puts "Category.create(:name => '#{cat.name}')"
end

Adjust for any additional fields you may have.

调整您可能拥有的任何其他字段。

Hope this helps.

希望这个有帮助。

#3


7  

I've used YamlDb to dump data from my development db and then load it up to another server. It dumps the data to a Yaml file, which will be used any time you want to use db:load to push it up to any other db server.

我使用YamlDb从开发数据库中转储数据,然后将其加载到另一个服务器。它将数据转储到Yaml文件,该文件将在任何时候使用db:load将其推到任何其他db服务器上。

https://github.com/ludicast/yaml_db

https://github.com/ludicast/yaml_db

#4


3  

Old question, I have a new one based on @Brian's answer.

老问题,我有一个新的基于@Brian的答案。

If you want to keep the entire row as is:

如果你想保持整行为:

seedfile = File.open('db/seeds.rb', 'a')

c = Category.all

c.each do |cat|
  seedfile.write "Category.create(#{cat.attributes})\n"
end

seedfile.close

If you want to only write some attributes, change the write line to the following:

如果您只想写一些属性,请将写行更改为以下内容:

seedfile.write "Category.create(#{cat.attributes.slice('attr1', 'attr2', ...})\n"

Or, if you wish all the attributes except some, for example timestamps:

或者,如果你想要所有的属性,除了一些,例如时间戳:

seedfile.write "Category.create(#{cat.attributes.except('created_at', 'updated_at')})\n"

#1


58  

There is a gem called seed_dump, which will do exactly what you want:

有一种叫做seed_dump的宝石,它可以做你想做的事情:

#2


27  

Not sure about any existing rake tasks, but you can try running something like this in the rails console & paste the results into your seeds.rb file

不确定现有的rake任务,但您可以尝试在rails控制台中运行类似的操作,并将结果粘贴到您的种子中。rb文件

(warning: dirty & untested)

(警告:脏和未测试)

c = Category.all

c.each do |cat|
  puts "Category.create(:name => '#{cat.name}')"
end

Adjust for any additional fields you may have.

调整您可能拥有的任何其他字段。

Hope this helps.

希望这个有帮助。

#3


7  

I've used YamlDb to dump data from my development db and then load it up to another server. It dumps the data to a Yaml file, which will be used any time you want to use db:load to push it up to any other db server.

我使用YamlDb从开发数据库中转储数据,然后将其加载到另一个服务器。它将数据转储到Yaml文件,该文件将在任何时候使用db:load将其推到任何其他db服务器上。

https://github.com/ludicast/yaml_db

https://github.com/ludicast/yaml_db

#4


3  

Old question, I have a new one based on @Brian's answer.

老问题,我有一个新的基于@Brian的答案。

If you want to keep the entire row as is:

如果你想保持整行为:

seedfile = File.open('db/seeds.rb', 'a')

c = Category.all

c.each do |cat|
  seedfile.write "Category.create(#{cat.attributes})\n"
end

seedfile.close

If you want to only write some attributes, change the write line to the following:

如果您只想写一些属性,请将写行更改为以下内容:

seedfile.write "Category.create(#{cat.attributes.slice('attr1', 'attr2', ...})\n"

Or, if you wish all the attributes except some, for example timestamps:

或者,如果你想要所有的属性,除了一些,例如时间戳:

seedfile.write "Category.create(#{cat.attributes.except('created_at', 'updated_at')})\n"