stanley90
stanley90

Reputation: 133

ActiveRecord - copy data from another database (without models)

I'm trying to connect to an old database to copy/process data to the current database (by Rails environment) (both sqlite). I want to execute raw SQL on the old one and create models in the new one, but I need the models to ignore the old db.

old_db = ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'storage/old.sqlite3')
old_db.with_connection do |c|
  c.execute('SELECT * FROM batches').each do |row|
    # want to Batch.create(...), but
    puts Batch.count # this is already non-zero because it uses the old db
  end
end

As a workaround I could load it to arrays, then switch the connection and insert, but is it possible in a single loop, just separating the connections somehow?

Upvotes: 0

Views: 20

Answers (0)

Related Questions