【发布时间】:2015-01-23 16:57:09
【问题描述】:
我有一个带有 Mongoid 数据库的 Rails 4 应用程序,我想引入一个沙盒环境来进行测试。有一些数据(两个模型),我想从生产数据库复制到沙箱。
我会使用 rake 任务来执行此操作,该任务由 cronjob 调用。但是,在这个 rake 任务中,我不确定如何建立到数据库的两个连接,并为不同的数据库使用相同的模型。
我也想在 mongodb 层做这件事(就像他们在How to copy a collection from one database to another in MongoDB 做的那样),但是一个模型由数据组成,应该只将部分复制到沙盒数据库中。因此,我认为我必须在 Rails 环境中进行。
在这个 rake 任务中,我可以将我所有的文章,但我不知道如何将它们“推送”到沙盒数据库中:
namespace :sandbox do
desc "Syncs production -> sandbox data"
task(:sync => :environment) do |t, args|
Article.all.each do |article|
if my_model1.state == :active
# here it should sync article to the sandbox models
# and then, it should also sync all the comments to the sandbox models
article.comments
end
end
end
end
end
【问题讨论】:
标签: ruby-on-rails ruby mongodb rake rake-task