如何使用seed.rb选择性地填充开发和/或生产数据库? [英] How to use seed.rb to selectively populate development and/or production databases?

查看:103
本文介绍了如何使用seed.rb选择性地填充开发和/或生产数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用seed.rb来填充我的开发和生产数据库.通常,我会先填充虚拟数据,然后再填充我的应用需要运行的实际最少数据(例如,第一个用户,等等).

I am using seed.rb to populate both my development and production database. I usually populate the first with dummy data and the latter with the real minimal data that my app needs to run (e.g. the first user and so on).

如何在seed.rb中指定每个数据的环境?

How can I specify in seed.rb for what environment each data is?

鉴于我知道分组"是一种Gemfile方法,所以我希望实现seed.rb的相同行为.

Given that I know "group" to be a Gemfile method, I'd like to achieve the same behavior for seed.rb.

例如我想在seed.rb中写这样的东西:

E.g. I'd like to write something like this in my seed.rb:

group :development do 
  # development specific seeding code
end

group :production do 
  # production specific seeding code
end

# non-specific seeding code (it always runs) 

这样既可以调用特定于开发的代码,也可以使用非特定代码

This to be able to call both the development-specific and the non-specific code with

$ rake db:seed

并通过以下方式调用生产专用代码和非生产专用代码:

And to call both the production-specific and the non-specific code with:

$ rake db:seed RAILS_ENV=production 

谢谢

推荐答案

seeds.rb只是一个普通的红宝石文件,因此有几种方法可以解决此问题.案件陈述怎么样?

seeds.rb is just a plain ruby file, so there are several ways you could approach this. How about a case statement?

# do common stuff here

case Rails.env
when "development"
   ...
when "production"
   ...
end

这篇关于如何使用seed.rb选择性地填充开发和/或生产数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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