Ruby On Rails:为环境创建不同种子文件的方法 [英] Ruby On Rails: way to create different seeds file for environments
本文介绍了Ruby On Rails:为环境创建不同种子文件的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何让任务rake db:seed
在生产和开发中使用不同的seeds.rb文件?
How can one make the task rake db:seed
to use different seeds.rb file on production and development?
欢迎任何更好的策略
推荐答案
你可以让 rake 任务根据当前环境有不同的行为,你可以通过传递 RAILS_ENV=production<来改变任务运行的环境/code> 到命令.将这两者结合使用,您可以产生如下结果:
You can have a rake task behave differently based on the current environment, and you can change the environment a task runs in by passing RAILS_ENV=production
to the command. Using these two together you could produce something like so:
使用您的环境特定种子创建以下文件:
Create the following files with your environment specific seeds:
db/seeds/development.rb
db/seeds/test.rb
db/seeds/production.rb
将此行放在您的基本种子文件中以运行所需的文件
Place this line in your base seeds file to run the desired file
load(Rails.root.join( 'db', 'seeds', "#{Rails.env.downcase}.rb"))
调用种子任务:
rake db:seed RAILS_ENV=production
这篇关于Ruby On Rails:为环境创建不同种子文件的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文