【发布时间】:2016-05-11 22:47:59
【问题描述】:
我正在构建一个 rake 任务,以便在两个数据库之间迁移一些数据。它们具有完全相同的结构。我的任务是这样的:
namespace :oab_nexus_migration do
task :start, [:oab_user, :nexus_user] => :environment do |t, args|
oab_account = User.find_by_username(args[:oab_user]).main_account
oab_trials = oab_account.trials.includes(:parts)
oab_schedules = oab_account.schedules
oab_movements = oab_account.movements
oab_annotations = oab_account.annotations
oab_hearings = oab_account.hearings
oab_publications = oab_account.publications
oab_tasks = oab_account.tasks
oab_people = oab_account.people.includes(:addresses, :internet_addresses, :phones)
ActiveRecord::Base.establish_connection(:other_database)
...
More code here
end
end
任务执行时:
RAILS_ENV=production rake oab_nexus_migration:start[user_one, user_two]
从数据库一中检索到我需要的所有信息后,我需要将其插入到数据库二中。但真正奇怪的事情正在发生。例如,如果我在establish_connection 之前调用p oab_trials(或任何其他变量),则所有值都在那里,由一个大数组表示。但是如果我尝试在establish_connection 之后调用它,返回的值是一个空数组。似乎 ActiveRecord 正在重置我之前定义的所有变量。
这里发生了什么?
【问题讨论】:
-
您可能想要制作两个不同但相似的模型来移植数据,每个数据库一个。
标签: ruby-on-rails ruby ruby-on-rails-3 activerecord