ActiveRecord正在重置我之前定义的所有变量

时间:2016-05-11 22:48:00

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord

我正在构建一个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]

从数据库一中检索到我需要的所有信息后,我需要将其插入到数据库二中。但是真的很奇怪。例如,如果我在p oab_trials之前调用establish_connection(或任何其他变量),则所有值都在那里,由大数组表示。但是如果我尝试在establish_connection之后调用它,则返回的值是一个空数组。好像ActiveRecord正在重置我以前定义的所有变量。

这里发生了什么?

2 个答案:

答案 0 :(得分:2)

oab_account.trials.includes(:parts)不会存储值,它会存储对延迟评估的ActiveRecord::Relation的引用。

因此,ActiveRecord不是重置变量的值,而是当您连接到第二个数据库时,您将引用更改为空集合。

答案 1 :(得分:2)

您引用的相关集合是ActiveRecord::Relation的惰性求值实例。如果您的数据库连接在流中更改,则延迟评估不起作用。

在切换连接之前,您需要先将这些关系加载到内存中。在关系上调用.to_a以强制执行此操作。

oab_account      = User.find_by_username(args[:oab_user]).main_account

oab_trials       = oab_account.trials.includes(:parts).to_a
oab_schedules    = oab_account.schedules.to_a
oab_movements    = oab_account.movements.to_a
oab_annotations  = oab_account.annotations.to_a
oab_hearings     = oab_account.hearings.to_a
oab_publications = oab_account.publications.to_a
oab_tasks        = oab_account.tasks.to_a
oab_people       = oab_account.people.includes(:addresses, :internet_addresses, :phones).to_a