mercredi 11 mai 2016

ActiveRecord is resetting all my previously defined variables

I'm building a rake task in order to migrate some data between two databases. The ones have exactly the same structure. My task is so:

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

The task is executed with:

RAILS_ENV=production rake oab_nexus_migration:start[user_one, user_two]

After retrieve all information I need from database one, I need to insert it into database two. But something really weird is happening. If I, for example, call p oab_trials (or any other variable) before establish_connection, all values are there, represented by a large array. But if I try to call it after establish_connection, the returned value is an empty array. Seems like ActiveRecord is resetting all my previously defined variables.

What is happening here?

Aucun commentaire:

Enregistrer un commentaire