ActiveRecord::Base.establish_connection 连接到错误的数据库

约翰

我创建了一个连接到 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.ymldevelopment并且test它可以工作时,但输出的用户来自另一个本地 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
建议

似乎您正在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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

连接到两个 sqlserver 数据库时,ActiveRecord::Base 不属于从 ActiveRecord 降序的层次结构

来自分类Dev

ActiveRecord尝试使用rbenv连接到错误的数据库

来自分类Dev

ActiveRecord :: Base.connection.execute(sql)。每个都不返回数据

来自分类Dev

Castle ActiveRecord未连接到数据库

来自分类Dev

使用ActiveRecord :: Base.connection的正确方法

来自分类Dev

ActiveRecord数据库连接的回调?

来自分类Dev

ActiveRecord :: Base.connection.table_structure(“ <表名>”)的Rails控制台抛出错误

来自分类Dev

ActiveRecord :: Base.connection.execute的受影响的行

来自分类Dev

使用ActiveRecord :: Base.connection返回哈希而不是数组

来自分类Dev

ActiveRecord是否按模型建立新的数据库连接

来自分类Dev

扩展ActiveRecord :: Base

来自分类Dev

扩展ActiveRecord :: Base

来自分类Dev

覆盖ActiveRecord :: Base

来自分类Dev

无法连接到数据库(HY000 / 1045),尝试使用php mysqli_connection连接到数据库

来自分类Dev

pqxx:尝试连接到 posgres 数据库时发生了 Broken_connection

来自分类Dev

如何成功继承ActiveRecord :: Base?

来自分类Dev

连接到MySQL数据库错误

来自分类Dev

mongoengine.connection.ConnectionError:无法连接到数据库默认值:[Errno 111]连接被拒绝

来自分类Dev

如何在使用ActiveRecord和多个线程时管理打开和关闭数据库连接

来自分类Dev

ActiveRecord :: ConnectionTimeoutError:在5.000秒(等待的5.000秒)内无法获得数据库连接

来自分类Dev

如何使用mysql2 gem配置ActiveRecord数据库连接

来自分类Dev

如何使用 ActiveRecord Rails 5 打开和关闭新的数据库连接

来自分类Dev

ActiveRecord :: StatementInvalid:PG ::错误:错误:必须是数据库的所有者

来自分类Dev

ActiveRecord(不带滑轨)create_database引发错误“数据库不存在”

来自分类Dev

水豚错误ActiveRecord :: StatementInvalid:SQLite3 :: BusyException:数据库已锁定

来自分类Dev

如何传递ActiveRecord :: Base类的哈希值

来自分类Dev

将变量添加到ActiveRecord :: Base

来自分类Dev

扩展ActiveRecord :: Base无法正常工作

来自分类Dev

在模型之间共享ActiveRecord :: Base枚举

Related 相关文章

  1. 1

    连接到两个 sqlserver 数据库时,ActiveRecord::Base 不属于从 ActiveRecord 降序的层次结构

  2. 2

    ActiveRecord尝试使用rbenv连接到错误的数据库

  3. 3

    ActiveRecord :: Base.connection.execute(sql)。每个都不返回数据

  4. 4

    Castle ActiveRecord未连接到数据库

  5. 5

    使用ActiveRecord :: Base.connection的正确方法

  6. 6

    ActiveRecord数据库连接的回调?

  7. 7

    ActiveRecord :: Base.connection.table_structure(“ <表名>”)的Rails控制台抛出错误

  8. 8

    ActiveRecord :: Base.connection.execute的受影响的行

  9. 9

    使用ActiveRecord :: Base.connection返回哈希而不是数组

  10. 10

    ActiveRecord是否按模型建立新的数据库连接

  11. 11

    扩展ActiveRecord :: Base

  12. 12

    扩展ActiveRecord :: Base

  13. 13

    覆盖ActiveRecord :: Base

  14. 14

    无法连接到数据库(HY000 / 1045),尝试使用php mysqli_connection连接到数据库

  15. 15

    pqxx:尝试连接到 posgres 数据库时发生了 Broken_connection

  16. 16

    如何成功继承ActiveRecord :: Base?

  17. 17

    连接到MySQL数据库错误

  18. 18

    mongoengine.connection.ConnectionError:无法连接到数据库默认值:[Errno 111]连接被拒绝

  19. 19

    如何在使用ActiveRecord和多个线程时管理打开和关闭数据库连接

  20. 20

    ActiveRecord :: ConnectionTimeoutError:在5.000秒(等待的5.000秒)内无法获得数据库连接

  21. 21

    如何使用mysql2 gem配置ActiveRecord数据库连接

  22. 22

    如何使用 ActiveRecord Rails 5 打开和关闭新的数据库连接

  23. 23

    ActiveRecord :: StatementInvalid:PG ::错误:错误:必须是数据库的所有者

  24. 24

    ActiveRecord(不带滑轨)create_database引发错误“数据库不存在”

  25. 25

    水豚错误ActiveRecord :: StatementInvalid:SQLite3 :: BusyException:数据库已锁定

  26. 26

    如何传递ActiveRecord :: Base类的哈希值

  27. 27

    将变量添加到ActiveRecord :: Base

  28. 28

    扩展ActiveRecord :: Base无法正常工作

  29. 29

    在模型之间共享ActiveRecord :: Base枚举

热门标签

归档