ActiveRecord :: Base。Establishment_connection连接到错误的数据库

时间:2018-08-22 07:34:00

标签: ruby-on-rails activerecord

我创建了一个可连接2个数据库的类:

class Production < ActiveRecord::Base
  ActiveRecord::Base.establish_connection(:production)
  self.abstract_class = true
  attr_accessor
end

class Backup < ActiveRecord::Base
  ActiveRecord::Base.establish_connection(:backup)
  self.abstract_class = true
  attr_accessor
end

class RepairReport
  def init

  end

  def repair_now
    Production.table_name = "users"
    users = Production.all
    users.each do |user|
      puts "USER: #{user.last_name}"
    end
  end
end

这是database.yml

production:
  adapter: postgresql
  encoding: unicode
  database: test_eagle_development
  host: localhost
  pool: 296
  username: postgres
  password: password1
  template: template0

backup:
  adapter: postgresql
  encoding: unicode
  database: test_eagle_test
  host: localhost
  pool: 5
  username: postgres
  password: password1
  template: template0

运行此命令时,出现错误:ActiveRecord::AdapterNotSpecified: 'production' database is not configured. Available: ["development", "test"]

当我将database.yml更改为developmenttest时,它可以工作,但是输出的用户来自另一个本地Rails应用程序中使用的另一个数据库。看来旧的连接仍在活动中?如何确定正确的数据库已连接?

更新:

现在这是我更新的database.yml和代码(请参见下文),但是它仍然连接到错误的数据库。当我删除developmenttest部分时,它返回:ActiveRecord::AdapterNotSpecified: 'development' database is not configured. Available: ["development_prod", "development_backup"],因此似乎在读正确的database.yml

development:
  adapter: postgresql
  encoding: unicode
  database: test_eagle_development
  host: localhost
  pool: 296
  username: postgres
  password: password1
  template: template0

test:
  adapter: postgresql
  encoding: unicode
  database: test_eagle_test
  host: localhost
  pool: 5
  username: postgres
  password: password1
  template: template0

development_prod:
  adapter: postgresql
  encoding: unicode
  database: test_eagle_development
  host: localhost
  pool: 296
  username: postgres
  password: password1
  template: template0

development_backup:
  adapter: postgresql
  encoding: unicode
  database: test_eagle_test
  host: localhost
  pool: 5
  username: postgres
  password: password1
  template: template0

和代码:

  class Production < ActiveRecord::Base
    ActiveRecord::Base.establish_connection("#{Rails.env}_prod".to_sym)
    self.abstract_class = true
    attr_accessor
  end

  class Backup < ActiveRecord::Base
    ActiveRecord::Base.establish_connection("#{Rails.env}_backup".to_sym)
    self.abstract_class = true
    attr_accessor
  end

1 个答案:

答案 0 :(得分:0)

似乎您正在development模式下运行Rails应用程序。 默认情况下,rails尝试加载数据库配置并通过RAILS_ENV值获取连接详细信息。

我建议为两个匹配环境命名连接:

development:
  adapter: postgresql
  encoding: unicode
  database: test_eagle_development
  host: localhost
  pool: 296
  username: postgres
  password: password1
  template: template0

development_backup:
  adapter: postgresql
  encoding: unicode
  database: test_eagle_test
  host: localhost
  pool: 5
  username: postgres
  password: password1
  template: template0
class Production < ActiveRecord::Base
  ActiveRecord::Base.establish_connection(Rails.env)
  self.abstract_class = true
end

class Backup < ActiveRecord::Base
  ActiveRecord::Base.establish_connection("#{Rails.env}_backup")
  self.abstract_class = true
end