最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ruby on rails - ActiveRecord - copy data from another database (without models) - Stack Overflow

programmeradmin0浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论