附加到 rake db:seed in rails 并运行它而不复制数据 [英] appending to rake db:seed in rails and running it without duplicating data

查看:40
本文介绍了附加到 rake db:seed in rails 并运行它而不复制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rake db:seed 使用应用程序的默认数据库值填充您的数据库,对吗?那么,如果您已经有一个种子并且需要向其中添加(您添加一个需要该种子的新功能),该怎么办?根据我的经验,当我再次运行 rake db:seed 时,它已经添加了现有内容,因此现有内容变成了两倍.

Rake db:seed populates your db with default database values for an app right? So what if you already have a seed and you need to add to it(you add a new feature that requires the seed). In my experience, when I ran rake db:seed again, it added the existing content already so existing content became double.

我需要的是添加一些种子,当运行时,它应该只添加最新的种子,而忽略现有的种子.我该怎么办?(我通常做的肮脏、菜鸟的做法是截断我的整个数据库,然后再次运行种子,但这在生产中并不是很聪明,对吧?)

What I need is to add some seeds and when ran, it should just add the newest ones, and ignore the existing seeds. How do I go about with this? (the dirty, noob way I usually do it is to truncate my whole db then run seed again, but that's not very smart to do in production, right?)

推荐答案

我就是这样做的.... 当我需要添加用户时

I do something like this.... When I need to add a user

在seeds.rb中:

in seeds.rb:

if User.count == 0
  puts "Creating admin user"
  User.create(:role=>:admin, :username=>'blagh', :etc=>:etc)
end

你可以得到比这更有趣的东西,但在这种情况下,你可以根据需要重新运行它.

You can get more interesting than that, but in this case, you could run it over again as needed.

这篇关于附加到 rake db:seed in rails 并运行它而不复制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆